HTTPie執行請求,惟一須要指定的信息是URL。默認的scheme,不出意外的,http://,是能夠忽略的,http example.org 也是能夠的。html
此外,像curl同樣的速記方式也是支持的。好比:3000也就是http://localhost:3000 若是忽略了端口,默認使用80python
$ http :/foo
GET /foo HTTP/1.1 Host: localhost
$ http :3000/bar
GET /bar HTTP/1.1 Host: localhost:3000
$ http :
GET / HTTP/1.1 Host: localhost
若是須要在終端手動用查詢字符串參數,能夠用param=value的語法拼接URL參數,而沒必要擔憂會丟失&分隔符。並且,參數裏的特殊字符也會自動去除。web
能夠用 --default-scheme <URL_SCHEME>選項來建立其餘協議的快捷方式json
$ alias https='http --default-scheme=https'
有幾種不一樣的request類型能夠方便的指定HTTP頭,JSON格式的數據,FORM數據,文件以及URL參數。在URL後面指定鍵值對。api
這些請求項都有共同的特色,是實際請求的一部分,它們僅僅以分隔符區分,:
, =
, :=
, ==
, @
, =@
, and :=@,
有@符號的是指定一個文件的路徑bash
Item Type | Description |
---|---|
HTTP Headersapp
|
任意 HTTP header, e.g. X-API-Token:123 . |
URL parameterspost
|
添加鍵值對做爲URL的查詢字符串參數 The |
Data Fields
|
請求數據字段默認做爲JSON對象傳遞, 或者做爲form-encoded ( |
Raw JSON fields
|
有時傳遞JSON,有些字段類型是 e.g., |
Form File Fields
|
僅僅剛使用 For example |
You can use \
to escape characters that shouldn't be used as separators (or parts thereof). For instance,foo\==bar
will become a data key/value pair (foo=
and bar
) instead of a URL parameter.
有時須要用到引號把值引發來 e.g. foo='bar baz'
.
若是字段名是以減號開始的‘-’(e.g., -fieldname
), 須要把它們放在 -- 後面以免衝突
--arguments
:
$ http httpbin.org/post -- -name-starting-with-dash=foo --Weird-Header:bar
POST /post HTTP/1.1 --Weird-Header: bar { "-name-starting-with-dash": "value" }
注意數據字段並非傳遞request data的惟一方式,Redirected input也能夠傳遞惟一的數據
JSON是現代web服務的通用語言,也是HTTPie默認使用的隱含類型:
若是命令行中包含數據項,它們默認序列化爲JSON格式。HTTPie會自動設置下列頭,它們也都是能夠重寫的:
Content-Type | application/json |
Accept | application/json, */* |
也能夠用--json, -j顯式的把Accept設置成application/json,無論是否須要傳遞數據(有一種快捷方式設置頭,經過經常使用的頭標識符 http url Accept:'application/json, */*')
此外,HTTPie會檢測JSON響應,即便Content-Type並不正確(好比text/plain)或者沒有設置。
例子:
$ http PUT example.org name=John email=john@example.org
PUT / HTTP/1.1 Accept: application/json, */* Accept-Encoding: gzip, deflate Content-Type: application/json Host: example.org { "name": "John", "email": "john@example.org" }
非string的字段要用:=分隔符,能夠把純JSON插入對象裏。Text和純JSON文件能夠分別用=@和:=@標識符插入:
$ http PUT api.example.com/person/1 \ name=John \ age:=29 married:=false hobbies:='["http", "pies"]' \ # Raw JSON description=@about-john.txt \ # Embed text file bookmarks:=@bookmarks.json # Embed JSON file
PUT /person/1 HTTP/1.1 Accept: application/json, */* Content-Type: application/json Host: api.example.com { "age": 29, "hobbies": [ "http", "pies" ], "description": "John is a nice guy who likes pies.", "married": false, "name": "John", "bookmarks": { "HTTPie": "http://httpie.org", } }
用文件形式傳遞JSON數據用以下形式:
$ http POST api.example.com/person/1 < person.json
$ http --form POST api.example.org/person/1 name='John Smith'
POST /person/1 HTTP/1.1 Content-Type: application/x-www-form-urlencoded; charset=utf-8 name=John+Smith
提供的一個以上的字段的話,序列化的content type就是multipart/form-data
$ http -f POST example.com/jobs name='John Smith' cv@~/Documents/cv.pdf
上述請求和以下HTML表單提交的狀況同樣:
<form enctype="multipart/form-data" method="post" action="http://example.com/jobs"> <input type="text" name="name" /> <input type="file" name="cv" /> </form>
注意@符號用於模擬文件上傳的表單字段,而=@符號只是把文件內容做爲text插入
若是要設置自定義的頭能夠用Header:Value標誌符
$ http example.org User-Agent:Bacon/1.0 'Cookie:valued-visitor=yes;foo=bar' X-Foo:Bar Referer:http://httpie.org/
GET / HTTP/1.1 Accept: */* Accept-Encoding: gzip, deflate Cookie: valued-visitor=yes;foo=bar Host: example.org Referer: http://httpie.org/ User-Agent: Bacon/1.0 X-Foo: Bar
HTTPie默認設置了須要頭信息
GET / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
User-Agent: HTTPie/<version>
Host: <taken-from-URL>
默認的頭能夠重寫也能夠清除掉,清除已指定的頭(好比一個默認的頭),用Header:
$ http httpbin.org/headers Accept: User-Agent:
若是要傳空值的頭,用Header;
$ http httpbin.org/headers 'Header;'