[譯]httpie: CLI, 類似cURL的用戶端

來源:互聯網
上載者:User

標籤:

5 Request URL

HTTPie執行請求,唯一需要指定的資訊是URL。預設的scheme,不出意外的,http://,是可以忽略的,http example.org 也是可以的。

此外,像curl一樣的速記方式也是支援的。比如:3000也就是http://localhost:3000 如果忽略了連接埠,預設使用80

$ http :/foo
GET /foo HTTP/1.1Host: localhost
$ http :3000/bar
GET /bar HTTP/1.1Host: localhost:3000
$ http :
GET / HTTP/1.1Host: localhost

如果需要在終端手動用查詢字串參數,可以用param=value的文法拼接URL參數,而不必擔心會丟失&分隔字元。而且,參數裡的特殊字元也會自動去除。

可以用 --default-scheme <URL_SCHEME>選項來建立其他協議的捷徑

$ alias https=‘http --default-scheme=https‘

  

6 請求項

有幾種不同的request類型可以方便的指定HTTP頭,JSON格式的資料,FORM資料,檔案以及URL參數。在URL後面指定索引值對。

這些請求項都有共同的特點,是實際請求的一部分,它們僅僅以分隔字元區分,:, =, :=, ==, @, [email protected], and :[email protected],有@符號的是指定一個檔案的路徑

Item Type Description

HTTP Headers

Name:Value

任意 HTTP header, e.g. X-API-Token:123.

URL parameters

name==value

添加索引值對作為URL的查詢字串參數

The == separator is used.

Data Fields

field=value,[email protected]

請求資料欄位預設作為JSON對象傳遞, 或者作為form-encoded (--form, -f).

Raw JSON fields

field:=json,field:[email protected]

有時傳遞JSON,有些欄位類型是 Boolean,Number, 內嵌對象t, or an Array,

e.g., meals:=‘["ham","spam"]‘ or pies:=[1,2,3] (注意引號).

Form File Fields

[email protected]/dir/file

僅僅剛使用 --form, -f的時候有效.

For example [email protected]~/Pictures/img.png. 這種形式會導致一個 a multipart/form-data request.

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也可以傳遞唯一的資料 

7 JSON

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 protected]
PUT / HTTP/1.1Accept: application/json, */*Accept-Encoding: gzip, deflateContent-Type: application/jsonHost: example.org{    "name": "John",    "email": "[email protected]"}

非string的欄位要用:=分隔字元,可以把純JSON插入對象裡。Text和純JSON檔案可以分別用[email protected]和:[email protected]標識符插入:

$ http PUT api.example.com/person/1     name=John     age:=29 married:=false hobbies:=‘["http", "pies"]‘ \  # Raw JSON    [email protected] \   # Embed text file    bookmarks:[email protected]      # Embed JSON file
PUT /person/1 HTTP/1.1Accept: application/json, */*Content-Type: application/jsonHost: 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

  

8 Form8.1 規則form
$ http --form POST api.example.org/person/1 name=‘John Smith‘
POST /person/1 HTTP/1.1Content-Type: application/x-www-form-urlencoded; charset=utf-8name=John+Smith

 

8.2 檔案上傳form

提供的一個以上的欄位的話,序列化的content type就是multipart/form-data

$ http -f POST example.com/jobs name=‘John Smith‘ [email protected]~/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>

注意@符號用於類比檔案上傳的表單欄位,而[email protected]符號只是把檔案內容作為text插入

 

9. HTTP 頭

如果要設定自訂的頭可以用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.1Accept: */*Accept-Encoding: gzip, deflateCookie: valued-visitor=yes;foo=barHost: example.orgReferer: http://httpie.org/User-Agent: Bacon/1.0X-Foo: Bar

HTTPie預設設定了需要頭資訊

GET / HTTP/1.1Accept: */*Accept-Encoding: gzip, deflateUser-Agent: HTTPie/<version>Host: <taken-from-URL>

預設的頭可以重寫也可以清除掉,清除已指定的頭(比如一個預設的頭),用Header:

$ http httpbin.org/headers Accept: User-Agent:

如果要傳空值的頭,用Header;

$ http httpbin.org/headers ‘Header;‘

  

 

[譯]httpie: CLI, 類似cURL的用戶端

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.