既に記事があります。
APIクライアント開発時のモックに使えるhttpbinの紹介
本記事は自分でも使って見ようと思い、使って試した時の自分用作業メモ。
作業メモなので上記記事を見てもらったほうが良いです。
送信したパラメータとヘッダーを確認できる
簡単に確認できる。
curl -G "https://httpbin.org/get?param1=ABC¶m2=DEF"
{
"args": {
"param1": "ABC",
"param2": "DEF"
},
"headers": {
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "curl/7.61.1",
"X-Amzn-Trace-Id": "Root=xxxxx"
},
"origin": "xxxxx",
"url": "https://httpbin.org/get?param1=ABC¶m2=DEF"
}
GET 以外のメソッドでも可能とのことだが、エンドポイントが違うので注意。
curl -X POST -H "Content-Type: application/json" -d '{"Name":"toshihirock", "Age":"100"}' https://httpbin.org/post
{
"args": {},
"data": "{\"Name\":\"toshihirock\", \"Age\":\"100\"}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Content-Length": "35",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "curl/7.61.1",
"X-Amzn-Trace-Id": "Root=xxxxx"
},
"json": {
"Age": "100",
"Name": "toshihirock"
},
"origin": "xxxx",
"url": "https://httpbin.org/post"
}
https
ではなく、http
も可能。
curl -X POST -H "Content-Type: application/json" -d '{"Name":"toshihirock", "Age":"100"}' http://httpbin.org/post
{
"args": {},
"data": "{\"Name\":\"toshihirock\", \"Age\":\"100\"}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Content-Length": "35",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "curl/7.61.1",
"X-Amzn-Trace-Id": "Root=xxx"
},
"json": {
"Age": "100",
"Name": "toshihirock"
},
"origin": "xxxx",
"url": "http://httpbin.org/post"
}
任意のステータスコードをテストできる
HTTP レスポンスの任意のステータスコードを返却する。
ステータスコード 404
の確認
curl -i -G "https://httpbin.org/status/404"
HTTP/2 404
date: Sun, 04 Apr 2021 21:22:38 GMT
content-type: text/html; charset=utf-8
content-length: 0
server: gunicorn/19.9.0
access-control-allow-origin: *
access-control-allow-credentials: true
500
の確認
curl -i -G "https://httpbin.org/status/500"
HTTP/2 500
date: Sun, 04 Apr 2021 21:23:55 GMT
content-type: text/html; charset=utf-8
content-length: 0
server: gunicorn/19.9.0
access-control-allow-origin: *
access-control-allow-credentials: true
curl 7.61.1
だとデフォルと HTTP/2
だったので --http1.1
をつける
curl -i -G --http1.1 "https://httpbin.org/status/500"
HTTP/1.1 500 INTERNAL SERVER ERROR
Date: Sun, 04 Apr 2021 21:27:25 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 0
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
遅延レスポンスのテスト
curl -G "https://httpbin.org/delay/5" --max-time 2
curl: (28) Operation timed out after 2001 milliseconds with 0 bytes received