
之前在命令行下进行HTTP服务的调试和信息查看都是使用经典的cURL,不过前段时间发现一个交互更加友好的工具,就是HTTPie。
先放一个HTTPie官方的一个HTTPie VS cURL的图给大家看看。

如果你经常需要用终端以非交互模式访问网络服务器(比如,下载文件、测试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 httpieFedora,CentOS/RHEL
sudo yum install httpie或者使用python的方式来安装
sudo pip install --upgrade httpieMac OSX
brew install httpie安装开发版本
# Mac OSXbrew 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.1Accept: */*Accept-Encoding: gzip, deflateConnection: keep-aliveHost: xx.github.ioReferer: http://xx.github.ioUser-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='xx@email.com'POST
http -f POST xx.github.io name='xx xx' email='xx@email.com'-f 选项使http命令序列化数据字段,并将Content-Type设置为application/x-www-form-urlencoded;charset=utf-8
这个HTTP POST请求看起这样:
POST / HTTP/1.1Accept: */*Accept-Encoding: gzip, deflateConnection: keep-aliveContent-Length: 41Content-Type: application/x-www-form-urlencoded; charset=utf-8Host: xx.github.ioUser-Agent: HTTPie/0.9.2name=x+x&email=xx%40email.comHEAD
http HEAD xx.github.ioHEAD这个方法只会让服务器返回http response headers。
这个命令结果如下:
Request
HEAD / HTTP/1.1Accept: */*Accept-Encoding: gzip, deflateConnection: keep-aliveHost: xx.github.ioUser-Agent: HTTPie/0.9.2Response
HTTP/1.1 200 OKAccept-Ranges: bytesAccess-Control-Allow-Origin: *Age: 0Cache-Control: max-age=600Connection: keep-aliveContent-Encoding: gzipContent-Length: 36794Content-Type: text/html; charset=utf-8Date: Wed, 15 Jul 2015 09:26:22 GMTExpires: Wed, 15 Jul 2015 09:36:22 GMTLast-Modified: Sun, 12 Jul 2015 11:32:15 GMTServer: GitHub.comVary: Accept-EncodingVia: 1.1 varnishX-Cache: MISSX-Cache-Hits: 0X-Served-By: cache-fra1227-FRAX-Timer: S1436952382.011631,VS0,VE99JSON支持
HTTPie内置JSON的支持。事实上HTTPie默认使用的Content-Type就是application/json。因此,当你不指定Content-Type发送请求参数时,它们就会自动序列化为JSON对象。
http POST xx.github.io name='x x' email='xx@email.com'这个请求看起来就是这样:
POST / HTTP/1.1Accept: application/jsonAccept-Encoding: gzip, deflateConnection: keep-aliveContent-Length: 52Content-Type: application/jsonHost: xx.github.ioUser-Agent: HTTPie/0.9.2{ "email": "xx@email.com", "name": "Tx Dx"}输入重定向
HTTPie的另外一个友好特性就是输入重定向,你可以使用缓冲数据提供HTTP请求内容。例如:
http POST xx.github.io < my_info.json或:
echo '{"name": "Tony Deng","email": "xx@email.com"}' | http POST xx.github.io