Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

49. curlとpostmanに慣れる #224

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions 95IG7y3HHX7yl4/06_WEBの基礎/02_curlとpostmanに慣れる/課題.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# (curl)
> 以下のリクエストをcurlコマンドでhttpbinに送信してください
> curlコマンドをペアと比較して、なぜそのような書き方をしたのか、話し合ってみましょう

## 問題1
> カスタムヘッダーを加える(X-Test='hello')
> methodはGET
> URLはhttps://httpbin.org/headers

回答
```
curl -H "X-Test: hello" https://httpbin.org/headers
```

## 問題2
> Content-Typeは"application/json"
> methodはPOST
> bodyは {"name": "hoge"}
> URLはhttps://httpbin.org/post

回答
```
curl -X POST -H "Content-Type: application/json" -d '{"name": "hoge"}' https://httpbin.org/post

// -dオプションを使用すると、curlは自動的にPOSTリクエストを行うため、-X POSTは省略可能。だから以下でもOK。
curl -H "Content-Type: application/json" -d '{"name": "hoge"}' https://httpbin.org/post
```

## 問題3
> もう少し複雑なbodyを送信してみましょう。以下のようなネストしたデータをbodyに含めて、送信してください
> {userA: {name: "hoge", age: 29}}

回答
```
curl -X POST -H "Content-Type: application/json" -d '{"userA": {"name": "hoge", "age": 29}}' https://httpbin.org/post
```

## 問題4
> 「ごめんごめん、問題2のエンドポイント、まだapplication/jsonに対応してないから、Content-Typeはapplication/x-www-form-urlencodedで送ってもらえる?」と先輩に頼まれました

回答
```
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "name=hoge" https://httpbin.org/post
```

## (postman)
> postmanをインストールしてください
> 上記の課題(curlコマンド)と同じ結果を得られるよう、リクエストを全てpostmanで再現してください

やってみた。

## クイズ
> curlに関するクイズを作成してください

Q1. curlでHTTPレスポンスヘッダーの取得するには?
Q2. curl実行時の詳細をログ出力するには?
Q3. curlでDDosテストを行うにはどのようなスクリプトを書けば良い?

A1. `-I`をオプションを付ける。
```
$ curl -I http://対象のURL

// サンプル
$ curl -I https://httpbin.org/headers
HTTP/1.1 200 OK
Date: Mon, 21 Oct 2024 22:40:20 GMT
Content-Type: application/json
```

A2. `-v`をオプションを付ける。
```
$ curl -v http://対象のURL

// サンプル
$ curl -v https://httpbin.org/headers
* Host httpbin.org:443 was resolved.
* IPv6: (none)
* IPv4: 34.238.67.180, 44.196.139.143
* Trying 34.238.67.180:443...
* Connected to httpbin.org (34.238.67.180) port 443
* schannel: disabled automatic use of client certificate
* ALPN: curl offers http/1.1
* ALPN: server accepted http/1.1
* using HTTP/1.x
> GET /headers HTTP/1.1
> Host: httpbin.org
> User-Agent: curl/8.9.1
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 200 OK
< Date: Mon, 21 Oct 2024 22:44:53 GMT
< Content-Type: application/json
< Content-Length: 172
< Connection: keep-alive
< Server: gunicorn/19.9.0
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Credentials: true
<
< Access-Control-Allow-Credentials: true
<
<
{
"headers": {
"Accept": "*/*",
"Host": "httpbin.org",
{
"headers": {
"Accept": "*/*",
"Host": "httpbin.org",
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "curl/8.9.1",
"X-Amzn-Trace-Id": "Root=1-6716d964-52e7c1d653b2ebe15de8f82a"
}
}
* Connection #0 to host httpbin.org left intact
```

A3.
```
$ for((;;)){ curl -v --header "Connection: keep-alive" "http://対象のURL" ;}
```

> postmanに関するクイズを作成してください

Q1. postmanで、curlコマンドをimport、exportすることは出来る?


A1. 出来る。
https://learning.postman.com/docs/getting-started/importing-and-exporting/importing-curl-commands/