- httpbin.org
- A simple HTTP Request & Response Service.
- 最早是 RequestBin 可以用來測 POST request,但無法控制 response,所以才有 HttpBin 的出現;但 HttpBin 不支援 POST (現在已支援)
- requests/httpbin: HTTP Request & Response Service, written in Python + Flask. 額外提到 written in Python + Flask
- Announcing Httpbin.org — Kenneth Reitz (2011-06-12)
- Requests 的測試在實務上有些惱人,現有的 PostBin.org (現在都會導到 http://requestb.in/ ?) 只能用來測 HTTP POST,而且執行在 GAE 上,所以 httpbin.org 就這樣誕生了。
- 例如
curl http://httpbin.org/user-agent會得到{ "user-agent": "curl/7.43.0" }。 - HttpBin 會打包在 PyPI 上,用 Flask 開發。
def test_hello_world():
query_string = {'greeting': 'Hello, World!'}
resp = requests.get('http://httpbin/get', params=query_string)
resp.raise_for_status() # best practice
assert resp.json()['args']['greeting'] == 'Hello, World!'
- httpbin.org 首頁是用 Flasgger 產生的 API 文件,區分為 HTTP Methods、Auth、Status codes、Response formats、Cookies、Images、Redirects 及 Anything,可以分開學習。
- Request inspection - httpbin.org 底下有
/headers、/ip跟/user-agent,感覺是GET /anything的簡化版?
/anything 因為 "returns anything passed in request data" 的特定,又支援不同的 HTTP method,可以用來檢查往 server 端送出的資料是否正確,但無法安排特定的 response,因為型態固定是 application/json,結構也是固定的。例如:
$ curl https://httpbin.org/anything?key1=value1 --data key2=value2
{
"args": { <-- 收到的資料
"key1": "value1"
},
"data": "",
"files": {},
"form": {
"key2": "value2"
},
"headers": { <-- 收到的 header
"Accept": "*/*",
"Connection": "close",
"Content-Length": "11",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "curl/7.54.0"
},
"json": null,
"method": "POST",
"origin": "12.34.56.78",
"url": "https://httpbin.org/anything?key1=value1"
}
參考資料:
- Anything - httpbin.org
- Returns anything that is passed to request 看似 Request inspection 是它的簡化版?
- 支援 DELETE、GET、PATCH、POST、PUT,有
/anything跟/anything/{anything}兩種 endpoint? 只要以/anything開頭即可。
- Enable request logging · Issue #421 · requests/httpbin
- drichards-levtech: 想看 server side 收到什麼?
- sigmavirus24: (member)
/anythingalready returns the data as it is seen by HTTPbin => 這一點可以用來確認收到什麼!!
$ docker run --rm -d --name httpbin kennethreitz/httpbin
$ docker run --rm -it --link httpbin alpine
/ # apk add --no-cache curl
/ # curl -i http://httpbin/get
HTTP/1.1 200 OK
Server: gunicorn/19.9.0
Date: Fri, 28 Sep 2018 13:37:49 GMT
Connection: keep-alive
Content-Type: application/json
Content-Length: 175
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {},
"headers": {
"Accept": "*/*",
"Host": "httpbin",
"User-Agent": "curl/7.61.1"
},
"origin": "172.17.0.5",
"url": "http://httpbin/get"
}
參考資料:
- httpbin.org 一開始就講 Run locally:
$ docker run -p 80:80 kennethreitz/httpbin - kennethreitz/httpbin - Docker Hub 這裡什麼都沒講
Dockerfile以ubuntu:18.04為基礎,執行用CMD ["gunicorn", "-b", "0.0.0.0:80", "httpbin:app", "-k", "gevent"]- Tags 只有
latestXD
社群: