HTTPie 是一款非常易用且易于升级的 HTTP 客户端。它的发音为 "aitch-tee-tee-pie",并以 http 命令运行,是一个用 Python 编写的用于访问网络的命令行工具。
由于本操作指南是关于 HTTP 客户端的,你需要一个 HTTP 服务器来试用它;在本例中,是 httpbin.org,一个简单的开源 HTTP 请求和响应服务。httpbin.org 网站是测试 Web API 客户端以及仔细管理和显示请求和响应细节的强大方式,但现在我们将重点关注 HTTPie 的强大功能。
Wget 和 cURL 的替代方案
你可能听说过历史悠久的 Wget 或稍新的 cURL 工具,它们允许你从命令行访问网络。它们最初是为访问网站而编写的,而 HTTPie 则是为访问 Web API 而设计的。
网站请求旨在计算机和最终用户之间进行,用户阅读并响应他们所看到的内容。这不太依赖于结构化响应。然而,API 请求在两台计算机之间进行 结构化 调用。人类不参与其中,而像 HTTPie 这样的命令行工具的参数可以有效地处理这种情况。
安装 HTTPie
有几种安装 HTTPie 的方法。你可能可以从你的包管理器中获取它,无论你使用 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"
}
通过将 http 直接别名为虚拟环境内的命令,即使虚拟环境未激活,你也可以运行它。你可以将 alias 命令放在 .bash_profile 或 .bashrc 中,这样你就可以使用以下命令升级 HTTPie
$ ~/.virtualenvs/httpie/bin/pip install -U httpie
使用 HTTPie 查询网站
HTTPie 可以简化 API 的查询和测试。上面使用了运行它的一个选项,-b(也称为 --body)。如果没有它,HTTPie 将默认打印整个响应,包括标头
$ 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"
}
这在调试 API 服务时至关重要,因为许多信息都在标头中发送。例如,查看正在发送哪些 cookie 通常很重要。Httpbin.org 提供了通过 URL 路径设置 cookie(用于测试目的)的选项。以下命令将名为 opensource 的 cookie 设置为值 awesome
$ 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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>Redirecting...</title>
<h1>Redirecting...</h1>
<p>You should be redirected automatically to target URL:
<a href="https://open-source.net.cn/cookies">/cookies</a>. If not click the link.
请注意 Set-Cookie: opensource=awesome; Path=/ 标头。这表明你期望设置的 cookie 已正确设置,并且路径为 /。另请注意,即使你收到了 302 重定向,http 也没有跟随它。如果你想跟随重定向,你需要使用 --follow 标志显式地请求它
$ 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"
}
}
但现在你看不到原始的 Set-Cookie 标头了。为了查看中间回复,你需要使用 --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
打印主体是无趣的,因为你主要对 cookie 感兴趣。如果你想查看来自中间请求的标头,但查看来自最终请求的主体,你可以这样做:
$ 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 精确控制要打印的内容,并使用 --history-print 覆盖中间请求打印的内容。
使用 HTTPie 下载二进制文件
有时主体是非文本的,需要发送到可以由其他应用程序打开的文件
$ 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/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:28:13 GMT
Referrer-Policy: no-referrer-when-downgrade
Server: nginx
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
Downloading 34.75 kB to "jpeg.jpe"
Done. 34.75 kB in 0.00068s (50.05 MB/s)
试试看!这张照片很可爱。
使用 HTTPie 发送自定义请求
你还可以发送特定的标头。这对于需要非标准标头的自定义 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"
}
}
最后,如果你想发送 JSON 字段(虽然可以指定确切的内容),对于许多不太嵌套的输入,你可以使用快捷方式
$ 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"
}
下次你调试 Web API 时,无论是你自己的还是别人的,放下你的 cURL,拿起 HTTPie,这个用于 Web API 的命令行客户端。
2 条评论