HTTPie 小抄

11 min read

HTTPie installs http and https:

Hello World:

$ https httpie.io/hello

Custom HTTP method, HTTP headers and JSON data:

$ http PUT pie.dev/put X-API-Token:123 name=John

Submitting forms:

$ http -f POST pie.dev/post hello=World

See the request that is being sent using one of the output options:

$ http -v pie.dev/get

Build and print a request without sending it using offline mode:

$ http --offline pie.dev/post hello=offline

Use Github API to post a comment on an issue with authentication:

$ http -a USERNAME POST https://api.github.com/repos/httpie/httpie/issues/83/comments body=HTTPie is awesome! :heart:

Upload a file using redirected input:

$ http pie.dev/post < files/data.json

Download a file and save it via redirected output:

$ http pie.dev/image/png > image.png

Download a file wget style:

$ http --download pie.dev/image/png

Use named sessions to make certain aspects of the communication persistent between requests to the same host:

$ http --session=logged-in -a username:password pie.dev/get API-Key:123
$ http --session=logged-in pie.dev/headers

Set a custom Host header to work around missing DNS records:

$ http localhost:8000 Host:example.com

发送 POST 请求

echo '{ "user": { "name": "john", "age": 10 } }' | http httpbin.org/post

或者

http PUT localhost:8080/user <<<'{ "user": { "name": "john", "age": 10 }}'

JSON支持

HTTPie内置JSON的支持。事实上HTTPie默认使用的Content-Type就是application/json。因此,当你不指定Content-Type发送请求参数时,它们就会自动序列化为JSON对象。

http POST tonydeng.github.io name='Tony Deng' email='[email protected]'