httpie简介
httpie是一个python写的类curl的命令行工具,跨平台,支持python2和3,友好的高亮显示以及其他的特性,基于Requests
和Pygments
库编写
主要特性
- Expressive and intuitive syntax
- Formatted and colorized terminal output
- Built-in JSON support
- Forms and file uploads
- HTTPS, proxies, and authentication
- Arbitrary request data
- Custom headers
- Persistent sessions
- Wget-like downloads
- Python 2.6, 2.7 and 3.x support
- Linux, Mac OS X and Windows support
- Plugins
- Documentation
- Test coverage
基本使用
语法
|
|
简写就是:
|
|
METHOD
如果不带METHOD参数,这默认为GET(没有附带请求参数)或POST(附带请求参数,默认以json格式传输)
|
|
URL
默认协议为http://
,如果主机是localhost
,还可以如下简写:
|
|
另外可以使用param==value
语法像url添加参数,所产生的效果就是浏览器中通过&
连接的参数,注意区分POST方法所使用的param=value
语法
|
|
linux系统中可以通过
|
|
来创建更方便https的命令
Request items
Item Type | Description |
---|---|
HTTP Headers Name:Value |
Arbitrary HTTP header, e.g. X-API-Token:123 |
URL parameters name==value |
Appends the given name/value pair as a query string parameter to the URL. The == separator is used. |
Data Fields field=value , field=@file.txt |
Request data fields to be serialized as a JSON object (default), or to be form-encoded (--form, -f ). |
Raw JSON fields field:=json , field:=@file.json |
Useful when sending JSON and one or more fields need to be a Boolean , Number , nested Object , or an Array , e.g., meals:='["ham","spam"]' or pies:=[1,2,3] (note the quotes). |
Form File Fields field@/dir/file |
Only available with --form, -f . For example screenshot@~/Pictures/img.png . The presence of a file field results in a multipart/form-data request. |
JSON
param=value
格式的参数全部会转换成json格式传输,并且value全是字符串
|
|
|
|
Non-string fields use the :=
separator, which allows you to embed raw JSON into the resulting object. Text and raw JSON files can also be embedded into fields using =@
and :=@
:
|
|
|
|
Forms
Submitting forms is very similar to sending JSON requests. Often the only difference is in adding the --form, -f
option, which ensures that data fields are serialized as, and Content-Type
is set to, application/x-www-form-urlencoded; charset=utf-8
.
|
|
|
|
File upload forms
|
|
上面的效果和下面一样:
|
|
HTTP headers
|
|
|
|
There are a couple of default headers that HTTPie sets:
|
|
Authentication
Basic auth:
|
|
Digest auth:
|
|
HTTP redirects
By default, HTTP redirects are not followed and only the first response is shown. To instruct HTTPie to follow the Location
header of 30x
responses and show the final response instead, use the --follow, -F
option.
If you additionally wish to see the intermediary requests/responses, then use the --all
option as well.
To change the default limit of maximum 30 redirects, use the --max-redirects=<limit>
option.
|
|
Proxies
|
|
SOCKS
|
|
HTTPS
可以通过--verify=no
忽略证书检查
|
|
可以使用--ssl=<PROTOCOL>
制定ssl版本
|
|
Output options
Param | Description |
---|---|
--headers, -h |
Only the response headers are printed. |
--body, -b |
Only the response body is printed. |
--verbose, -v |
Print the whole HTTP exchange (request and response). This option also enables --all (see bellow). |
--print, -p |
Selects parts of the HTTP exchange. |
Character | Stands for |
---|---|
H |
request headers |
B |
request body |
h |
response headers |
b |
response body |
|
|
Redirected output
Download a file:
|
|
Download mode
HTTPie features a download mode in which it acts similarly to wget
.
When enabled using the --download, -d
flag, response headers are printed to the terminal (stderr
), and a progress bar is shown while the response body is being saved to a file.
|
|
|
|
Sessions
By default, every request is completely independent of any previous ones. HTTPie also supports persistent sessions, where custom headers (except for the ones starting with Content-
or If-
), authorization, and cookies (manually specified or sent by the server) persist between requests to the same host.
Named sessions
Create a new session named user1
for example.org
:
|
|
Now you can refer to the session by its name, and the previously used authorization and HTTP headers will automatically be set:
|
|