HTTPie:超爽的HTTP命令行客戶端

HTTPie:超爽的HTTP命令行客戶端

之前在命令行下進行HTTP服務的調試和信息查看都是使用經典的cURL,不過前段時間發現一個交互更加友好的工具,就是HTTPie。

先放一個HTTPie官方的一個HTTPie VS cURL的圖給大家看看。

HTTPie:超爽的HTTP命令行客戶端

如果你經常需要用終端以非交互模式訪問網絡服務器(比如,下載文件、測試REST服務),可能你會選擇的工具是wget或curl,通過大量的選項,這兩種工具也都可以處理很多非交互網絡訪問的情況(比如,如何通過wget下載多個文件,如何在wget中設置自定義的http頭,如何在curl中設置自定義的HTTP頭)。然而,除非你很精通這些繁複的語法細節,這些工具對於你來說,不過是簡單的下載工具而已。

“Its goal is to make CLI interaction with web services as human-friendly as possible. It provides a simple http command that allows for sending arbitrary HTTP requests using a simple and natural syntax, and displays colorized output. “

HTTPie則在使用時的表現力、人性化做得比wget、curl好得多,就像在官網上宣傳的那樣,它追求的是人性化、簡單自然的語法,以及彩色的輸出。而且HTTPie還有一些不錯的優點,比如對JSON的良好支撐、持久性會話等特性。

上面說了那麼多,咱們廢話少說,先來了解並展示一下HTTPie。

安裝

Linux上安裝

Debian, Ubuntu或Linux Mint

sudo apt-get install httpie

Fedora,CentOS/RHEL

sudo yum install httpie

或者使用python的方式來安裝

sudo pip install --upgrade httpie

Mac OSX

brew install httpie

安裝開發版本

# Mac OSX
brew install httpie --HEAD
# Python安裝方式
pip install --upgrade https://github.com/jkbrzt/httpie/tarball/master

使用例子

定製頭部

http xx.github.io/blog/2015/07/10/httpie-howto/ User-Agent:Xmodlo/1.0 Referer:http://xx.github.io

這個HTTP請求看起是這樣。

GET /blog/2015/07/10/httpie-howto/ HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: xx.github.io
Referer: http://xx.github.io
User-Agent: Xmodlo/1.0

下載文件

你也可以把HTTPie作為文件下載器來使用。

http xx.github.io/blog/2015/07/10/httpie-howto/ > httpie-howto.html

或者

http --download xx.github.io/blog/2015/07/10/httpie-howto/

使用其他HTTP方法

除了默認的GET方法,你還可以使用其他方法(比如PUT、POST、DELETE、HEAD)

PUT

http PUT xx.github.io name='x x' email='[email protected]'

POST

http -f POST xx.github.io name='xx xx' email='[email protected]'

-f 選項使http命令序列化數據字段,並將Content-Type設置為application/x-www-form-urlencoded;charset=utf-8

這個HTTP POST請求看起這樣:

POST / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 41
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Host: xx.github.io
User-Agent: HTTPie/0.9.2
name=x+x&email=xx%40email.com

HEAD

http HEAD xx.github.io

HEAD這個方法只會讓服務器返回http response headers。

這個命令結果如下:

Request

HEAD / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: xx.github.io
User-Agent: HTTPie/0.9.2

Response

HTTP/1.1 200 OK
Accept-Ranges: bytes
Access-Control-Allow-Origin: *
Age: 0
Cache-Control: max-age=600
Connection: keep-alive

Content-Encoding: gzip
Content-Length: 36794
Content-Type: text/html; charset=utf-8
Date: Wed, 15 Jul 2015 09:26:22 GMT
Expires: Wed, 15 Jul 2015 09:36:22 GMT
Last-Modified: Sun, 12 Jul 2015 11:32:15 GMT
Server: GitHub.com
Vary: Accept-Encoding
Via: 1.1 varnish
X-Cache: MISS
X-Cache-Hits: 0
X-Served-By: cache-fra1227-FRA
X-Timer: S1436952382.011631,VS0,VE99

JSON支持

HTTPie內置JSON的支持。事實上HTTPie默認使用的Content-Type就是application/json。因此,當你不指定Content-Type發送請求參數時,它們就會自動序列化為JSON對象。

http POST xx.github.io name='x x' email='[email protected]'

這個請求看起來就是這樣:

POST / HTTP/1.1
Accept: application/json
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 52
Content-Type: application/json
Host: xx.github.io
User-Agent: HTTPie/0.9.2
{
"email": "[email protected]",
"name": "Tx Dx"
}

輸入重定向

HTTPie的另外一個友好特性就是輸入重定向,你可以使用緩衝數據提供HTTP請求內容。例如:

http POST xx.github.io < my_info.json

或:

echo '{"name": "Tony Deng","email": "[email protected]"}' | http POST xx.github.io


分享到:


相關文章: