新聞中心
HTTPie 是一個(gè) HTTP 的命令行客戶(hù)端,目標(biāo)是讓 CLI 和 web 服務(wù)之間的交互盡可能的人性化。這個(gè)工具提供了簡(jiǎn)潔的 http 命令,允許通過(guò)自然的語(yǔ)法發(fā)送任意 HTTP 請(qǐng)求數(shù)據(jù),展示色彩化的輸出。HTTPie 可用于與 HTTP 服務(wù)器做測(cè)試、調(diào)試和常規(guī)交互,下面為大家詳細(xì)講解一下HTTPie具體使用方法。

成都創(chuàng)新互聯(lián)公司專(zhuān)注于廉江企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,電子商務(wù)商城網(wǎng)站建設(shè)。廉江網(wǎng)站建設(shè)公司,為廉江等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站建設(shè),專(zhuān)業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專(zhuān)業(yè)和態(tài)度為您提供的服務(wù)
Wget 和 cURL 的替代品
你可能聽(tīng)說(shuō)過(guò)古老的 Wget 或稍微新一些的 cURL 工具,它們?cè)试S你從命令行訪問(wèn) Web。它們是為訪問(wèn)網(wǎng)站而編寫(xiě)的,而 HTTPie 則用于訪問(wèn) Web API。
網(wǎng)站請(qǐng)求發(fā)生在計(jì)算機(jī)和正在閱讀并響應(yīng)它所看到的內(nèi)容的最終用戶(hù)之間,這并不太依賴(lài)于結(jié)構(gòu)化的響應(yīng)。但是,API 請(qǐng)求會(huì)在兩臺(tái)計(jì)算機(jī)之間進(jìn)行結(jié)構(gòu)化調(diào)用,人并不是該流程內(nèi)的一部分,像 HTTPie 這樣的命令行工具的參數(shù)可以有效地處理這個(gè)問(wèn)題。
安裝 HTTPie
有幾種方法可以安裝 HTTPie。你可以通過(guò)包管理器安裝,無(wú)論你使用的是 brew、apt、yum 還是 dnf。但是,如果你已配置 virtualenvwrapper,那么你可以用自己的方式安裝:
$ mkvirtualenv httpie
...
(httpie) $ pip install httpie
...
(httpie) $ deactivate
$ alias http=~/.virtualenvs/httpie/bin/http
$ http -b GET https://httpbin.org/get
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "HTTPie/1.0.2"
},
"origin": "104.220.242.210, 104.220.242.210",
"url": "https://httpbin.org/get"
}
通過(guò)將 http 別名指向?yàn)樘摂M環(huán)境中的命令,即使虛擬環(huán)境在非活動(dòng)狀態(tài),你也可以運(yùn)行它。你可以將 alias 命令放在 .bash_profile 或 .bashrc 中,這樣你就可以使用以下命令升級(jí) HTTPie:
$ ~/.virtualenvs/httpie/bin/pip install -U pip
使用 HTTPie 查詢(xún)網(wǎng)站
HTTPie 可以簡(jiǎn)化查詢(xún)和測(cè)試 API。上面使用了一個(gè)選項(xiàng),-b(即 –body)。沒(méi)有它,HTTPie 將默認(rèn)打印整個(gè)響應(yīng),包括響應(yīng)頭:
$ http GET https://httpbin.org/get
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Encoding: gzip
Content-Length: 177
Content-Type: application/json
Date: Fri, 09 Aug 2019 20:19:47 GMT
Referrer-Policy: no-referrer-when-downgrade
Server: nginx
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "HTTPie/1.0.2"
},
"origin": "104.220.242.210, 104.220.242.210",
"url": "https://httpbin.org/get"
}
這在調(diào)試 API 服務(wù)時(shí)非常重要,因?yàn)榇罅啃畔⒃陧憫?yīng)頭中發(fā)送。例如,查看發(fā)送的 cookie 通常很重要。httpbin.org 提供了通過(guò) URL 路徑設(shè)置 cookie(用于測(cè)試目的)的方式。以下設(shè)置一個(gè)標(biāo)題為 opensource, 值為 awesome 的 cookie:
$ http GET https://httpbin.org/cookies/set/opensource/awesome
HTTP/1.1 302 FOUND
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 223
Content-Type: text/html; charset=utf-8
Date: Fri, 09 Aug 2019 20:22:39 GMT
Location: /cookies
Referrer-Policy: no-referrer-when-downgrade
Server: nginx
Set-Cookie: opensource=awesome; Path=/
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
"-//W3C//DTD HTML 3.2 Final//EN">
Redirecting...Redirecting...
You should be redirected automatically to target URL: "/cookies">/cookies. If not click the link.
注意 Set-Cookie: opensource=awesome; Path=/ 的響應(yīng)頭。這表明你預(yù)期設(shè)置的 cookie 已正確設(shè)置,路徑為 /。另請(qǐng)注意,即使你得到了 302 重定向,http 也不會(huì)遵循它。如果你想要遵循重定向,則需要明確使用 –follow 標(biāo)志請(qǐng)求:
$ http --follow GET https://httpbin.org/cookies/set/opensource/awesome
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Encoding: gzip
Content-Length: 66
Content-Type: application/json
Date: Sat, 10 Aug 2019 01:33:34 GMT
Referrer-Policy: no-referrer-when-downgrade
Server: nginx
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
{
"cookies": {
"opensource": "awesome"
}
}
但此時(shí)你無(wú)法看到原來(lái)的 Set-Cookie 頭。為了看到中間響應(yīng),你需要使用 –all:
$ http --headers --all --follow GET https://httpbin.org/cookies/set/opensource/awesome
HTTP/1.1 302 FOUND
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Content-Type: text/html; charset=utf-8
Date: Sat, 10 Aug 2019 01:38:40 GMT
Location: /cookies
Referrer-Policy: no-referrer-when-downgrade
Server: nginx
Set-Cookie: opensource=awesome; Path=/
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
Content-Length: 223
Connection: keep-alive
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Content-Encoding: gzip
Content-Type: application/json
Date: Sat, 10 Aug 2019 01:38:41 GMT
Referrer-Policy: no-referrer-when-downgrade
Server: nginx
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
Content-Length: 66
Connection: keep-alive
打印響應(yīng)體并不有趣,因?yàn)槟愦蠖鄶?shù)時(shí)候只關(guān)心 cookie。如果你想看到中間請(qǐng)求的響應(yīng)頭,而不是最終請(qǐng)求中的響應(yīng)體,你可以使用:
$ http --print hb --history-print h --all --follow GET https://httpbin.org/cookies/set/opensource/awesome
HTTP/1.1 302 FOUND
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Content-Type: text/html; charset=utf-8
Date: Sat, 10 Aug 2019 01:40:56 GMT
Location: /cookies
Referrer-Policy: no-referrer-when-downgrade
Server: nginx
Set-Cookie: opensource=awesome; Path=/
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
Content-Length: 223
Connection: keep-alive
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Content-Encoding: gzip
Content-Type: application/json
Date: Sat, 10 Aug 2019 01:40:56 GMT
Referrer-Policy: no-referrer-when-downgrade
Server: nginx
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
Content-Length: 66
Connection: keep-alive
{
"cookies": {
"opensource": "awesome"
}
}
你可以使用 –print 精確控制打印的內(nèi)容(h:響應(yīng)頭;b:響應(yīng)體),并使用 –history-print 覆蓋中間請(qǐng)求的打印內(nèi)容設(shè)置。
使用 HTTPie 下載二進(jìn)制文件
有時(shí)響應(yīng)體并不是文本形式,它需要發(fā)送到可被不同應(yīng)用打開(kāi)的文件:
$ http GET https://httpbin.org/image/jpeg
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 35588
Content-Type: image/jpeg
Date: Fri, 09 Aug 2019 20:25:49 GMT
Referrer-Policy: no-referrer-when-downgrade
Server: nginx
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
+-----------------------------------------+
| NOTE: binary data not shown in terminal |
+-----------------------------------------+
要得到正確的圖片,你需要保存到文件:
$ http --download GET https://httpbin.org/image/jpegHTTP/1.1 200 OKAccess-Control-Allow-Credentials: trueAccess-Control-Allow-Origin: *Connection: keep-aliveContent-Length: 35588Content-Type: image/jpegDate: Fri, 09 Aug 2019 20:28:13 GMTReferrer-Policy: no-referrer-when-downgradeServer: nginxX-Content-Type-Options: nosniffX-Frame-Options: DENYX-XSS-Protection: 1; mode=blockDownloading 34.75 kB to "jpeg.jpe"Done. 34.75 kB in 0.00068s (50.05 MB/s)
試一下!圖片很可愛(ài)。
使用 HTTPie 發(fā)送自定義請(qǐng)求
你可以發(fā)送指定的請(qǐng)求頭。這對(duì)于需要非標(biāo)準(zhǔn)頭的自定義 Web API 很有用:
$ http GET https://httpbin.org/headers X-Open-Source-Com:Awesome{ "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Host": "httpbin.org", "User-Agent": "HTTPie/1.0.2", "X-Open-Source-Com": "Awesome" }}
最后,如果要發(fā)送 JSON 字段(盡管可以指定確切的內(nèi)容),對(duì)于許多嵌套較少的輸入,你可以使用快捷方式:
$ http --body PUT https://httpbin.org/anything open-source=awesome author=moshez
{
"args": {},
"data": "{\"open-source\": \"awesome\", \"author\": \"moshez\"}",
"files": {},
"form": {},
"headers": {
"Accept": "application/json, */*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "46",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "HTTPie/1.0.2"
},
"json": {
"author": "moshez",
"open-source": "awesome"
},
"method": "PUT",
"origin": "73.162.254.113, 73.162.254.113",
"url": "https://httpbin.org/anything"
}
下次在調(diào)試 Web API 時(shí),無(wú)論是你自己的還是別人的,記得放下 cURL,試試 HTTPie 這個(gè)命令行工具。
網(wǎng)站標(biāo)題:詳解HTTPie具體使用方法
分享網(wǎng)址:http://www.5511xx.com/article/dhjioco.html


咨詢(xún)
建站咨詢(xún)
