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 条评论