httpie使用详解

语法:

http [--json] [--form] [--pretty {all,colors,format,none}]
     [--style STYLE] [--print WHAT] [--headers] [--body] [--verbose]
     [--all] [--history-print WHAT] [--stream] [--output FILE]
     [--download] [--continue]
     [--session SESSION_NAME_OR_PATH | --session-read-only SESSION_NAME_OR_PATH]
     [--auth USER[:PASS]] [--auth-type {basic,digest}]
     [--proxy PROTOCOL:PROXY_URL] [--follow]
     [--max-redirects MAX_REDIRECTS] [--timeout SECONDS]
     [--check-status] [--verify VERIFY]
     [--ssl {ssl2.3,ssl3,tls1,tls1.1,tls1.2}] [--cert CERT]
     [--cert-key CERT_KEY] [--ignore-stdin] [--help] [--version]
     [--traceback] [--default-scheme DEFAULT_SCHEME] [--debug]
     [METHOD] URL [REQUEST_ITEM [REQUEST_ITEM ...]]

简写就是:

$ http [flags] [METHOD] URL [ITEM [ITEM]]

METHOD
如果不带METHOD参数,这默认为GET(没有附带请求参数)或POST(附带请求参数,默认以json格式传输)

$ http example.org               # => GET
$ http example.org hello=world   # => POST

URL
默认协议为 http://,如果主机是localhost,还可以如下简写:

$ http :3000                    # => http://localhost:3000
$ http :/foo                    # => http://localhost/foo

另外可以使用param==value语法像url添加参数,所产生的效果就是浏览器中通过&连接的参数,注意区分POST方法所使用的param=value语法

http www.google.com search=='HTTPie logo' tbm==isch

创建更方便https的命令

alias https='http --default-scheme=https'

Request items

Item Type Description e.g
HTTP Headers Name:Value Arbitrary HTTP header 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, [email protected] Request data fields to be serialized as a JSON object (default), or to be form-encoded (--form, -f). ``
Raw JSON fields field:=json, field:[email protected] Useful when sending JSON and one or more fields need to be a Boolean, Number, nested Object, or an Array meals:='["ham","spam"]' or pies:=[1,2,3] (note the quotes).
Form File Fields field@/dir/file Only available with --form, -f. The presence of a file field results in a multipart/form-data request. screenshot@~/Pictures/img.png.
``
``
``
``

json

param=value格式的参数全部会转换成json格式传输,并且value全是字符串

http POST httpbin.org/anything name=John [email protected] age=26

HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 575
Content-Type: application/json
Date: Tue, 05 Dec 2017 05:03:10 GMT
Server: meinheld/0.6.1
Via: 1.1 vegur
X-Powered-By: Flask
X-Processed-Time: 0.000778913497925

{
    "args": {},
    "data": "{\"name\": \"John\", \"email\": \"[email protected]\", \"age\": \"26\"}",
    "files": {},
    "form": {},
    "headers": {
        "Accept": "application/json, */*",
        "Accept-Encoding": "gzip, deflate",
        "Connection": "close",
        "Content-Length": "58",
        "Content-Type": "application/json",
        "Host": "httpbin.org",
        "User-Agent": "HTTPie/0.9.9"
    },
    "json": {
        "age": "26",
        "email": "[email protected]",
        "name": "John"
    },
    "method": "POST",
    "origin": "183.234.62.33",
    "url": "http://httpbin.org/anything"
}

非字符串字段则使用 := 分隔符, which allows you to embed raw JSON into the resulting object. Text and raw JSON files can also be embedded into fields using =@ and :=@:

 /tmp  cat addr.json
{
  "home":"home addr",
  "school":"school addr"
}
 /tmp  cat about.txt
this is about


http POST httpbin.org/anything name=John [email protected] \
> age:=29 married:=false hobbies:='["http", "pies"]' \  \  # Raw JSON
> [email protected] \                                       # Embed text file
> addr:[email protected]                                         # Embed JSON file
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 915
Content-Type: application/json
Date: Tue, 05 Dec 2017 05:10:58 GMT
Server: meinheld/0.6.1
Via: 1.1 vegur
X-Powered-By: Flask
X-Processed-Time: 0.00129795074463

{
    "args": {},
    "data": "{\"name\": \"John\", \"email\": \"[email protected]\", \"age\": 29, \"married\": false, \"hobbies\": [\"http\", \"pies\"], \"about\": \"this is about \\n\", \"addr\": {\"home\": \"home addr\", \"school\": \"school addr\"}}",
    "files": {},
    "form": {},
    "headers": {
        "Accept": "application/json, */*",
        "Accept-Encoding": "gzip, deflate",
        "Connection": "close",
        "Content-Length": "188",
        "Content-Type": "application/json",
        "Host": "httpbin.org",
        "User-Agent": "HTTPie/0.9.9"
    },
    "json": {
        "about": "this is about \n",
        "addr": {
            "home": "home addr",
            "school": "school addr"
        },
        "age": 29,
        "email": "[email protected]",
        "hobbies": [
            "http",
            "pies"
        ],
        "married": false,
        "name": "John"
    },
    "method": "POST",
    "origin": "183.234.62.33",
    "url": "http://httpbin.org/anything"
}

你可能感兴趣的:(httpie使用详解)