HTTPie for Terminal
:面向 API 时代的人性化 CLI HTTP 客户端,HTTPie
(发音为aitch-tee-tee-pie)是一个命令行 HTTP
客户端。它的目标是使 CLI 与 Web
服务的交互尽可能人性化。HTTPie
设计用于测试、调试以及通常与 API
和HTTP
服务器交互。http&https
命令允许创建和发送任意 HTTP
请求。它们使用简单自然的语法,并提供格式化和彩色输出。
安装之前确保自己的python
版本在 3.7以上。用python --version
可以参看python
的版本。
安装
# Install httpie
python -m pip install --upgrade pip wheel
python -m pip install httpie
更新
# Upgrade httpie
python -m pip install --upgrade pip wheel
python -m pip install --upgrade httpie
macOS
下的安装:
# Install httpie
brew update
brew install httpie
# Upgrade httpie
brew update
brew upgrade httpie
ubuntu
下的安装:
# Install httpie
curl -SsL https://packages.httpie.io/deb/KEY.gpg | apt-key add -
curl -SsL -o /etc/apt/sources.list.d/httpie.list https://packages.httpie.io/deb/httpie.list
apt update
apt install httpie
我们还是从最简单的hello world
开始:
https httpie.io/hello
得到的结果如下:
HTTP/1.1 200 OK
Age: 0
Cache-Control: public, max-age=0, must-revalidate
Connection: keep-alive
Content-Length: 264
Content-Type: application/json; charset=utf-8
Date: Wed, 26 Oct 2022 01:57:50 GMT
Etag: "108-yw+Xn8xsGAsJIUMQvDLN7gAcQuc"
Server: Vercel
Strict-Transport-Security: max-age=63072000
X-Matched-Path: /api/hello
X-Vercel-Cache: MISS
X-Vercel-Id: hnd1::iad1::9dtl2-1666749470755-c32d10172552
{
"ahoy": [
"Hello, World! Thank you for trying out HTTPie ",
"We hope this will become a friendship."
],
"links": {
"discord": "https://httpie.io/discord",
"github": "https://github.com/httpie",
"homepage": "https://httpie.io",
"twitter": "https://twitter.com/httpie"
}
}
命令的格式如下:
http [flags] [METHOD] URL [ITEM [ITEM]]
http
方法:
http -f POST pie.dev/post hello=World
结果如下:
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
CF-Cache-Status: DYNAMIC
CF-RAY: 75ffa69b3cde5281-LAX
Connection: keep-alive
Content-Encoding: gzip
Content-Type: application/json
Date: Wed, 26 Oct 2022 02:09:36 GMT
NEL: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
Report-To: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=1qOC9CYUvezMJFcoJDMgBy8jegWrUGL8gJKyFlFWQVYgp5%2BjUug8TqCMDPv%2FspfQOp%2FUn%2B9RBPHdyXGDEH7FTZAgkpLImt9%2BWo%2BDSbSKKct5xHnRtKTxKlZ7"}],"group":"cf-nel","max_age":604800}
Server: cloudflare
Transfer-Encoding: chunked
alt-svc: h3=":443"; ma=86400, h3-29=":443"; ma=86400
{
"args": {},
"data": "",
"files": {},
"form": {
"hello": "World"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip",
"Cdn-Loop": "cloudflare",
"Cf-Connecting-Ip": "125.77.202.250",
"Cf-Ipcountry": "CN",
"Cf-Ray": "75ffa69b3cde5281-FRA",
"Cf-Visitor": "{\"scheme\":\"http\"}",
"Connection": "Keep-Alive",
"Content-Length": "11",
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
"Host": "pie.dev",
"User-Agent": "HTTPie/3.2.1"
},
"json": null,
"origin": "125.77.202.250",
"url": "http://pie.dev/post"
}