如何安装 HTTPie CLI:GET/POST 示例、请求头与 JSON body 及与 curl 对照
安装 http 命令,完成 GET/POST 与 JSON 请求,并对照 curl 写法。
调试 REST API 时,curl 功能全但 flags 难记;HTTPie(命令 http)用更直观的语法发 GET/POST、设请求头、提交 JSON,并默认美化响应。本教程覆盖 macOS/Linux/Windows 安装、GET 与 POST 示例、自定义 Header 与 JSON body、与 curl 对照,面向 hereink 读者常用的 API 与脚本工作流。
检索词是如何安装 HTTPie CLI 并用 GET POST 发 JSON 请求。读完应能安装 http、对公开 API 发第一条 GET、用 JSON body POST,并读懂与 curl 的等价写法。
HTTPie 解决什么问题
比 curl 更易读的命令行
http GET api.example.com/users 把方法、URL、常见选项拆成可读片段;JSON 响应自动着色与缩进,少记 -H、-d 的组合顺序。
与 jq 管道搭档
响应体仍是 JSON 时,可 http GET … | jq '.data' 只留关心的字段。站内 jq 教程负责变换 JSON;HTTPie 负责发请求。
上游:
github.com/httpie/cli,文档httpie.io/docs/cli。安装后运行http --version确认。
安装 HTTPie
- 1macOS(Homebrew)
brew install httpie。验证:which http与http --version。 - 2Linux(发行版包或 pip)
Debian/Ubuntu:
sudo apt update && sudo apt install httpie。Fedora:sudo dnf install httpie。通用:python3 -m pip install --user httpie,确保~/.local/bin在PATH。 - 3Windows
PowerShell:
winget install httpie.httpie,或pip install httpie。新开终端后http --version。 - 4第一条 GET
http GET https://httpbin.org/get应返回 JSON,含请求头镜像字段。HTTPS 证书由系统信任库校验。
GET 与查询参数
# 简单 GET
http GET https://httpbin.org/get
# 查询参数(HTTPie 用 == 表示 URL 查询)
http GET https://httpbin.org/get page==2 limit==10
# 只打印响应体(脚本用)
http --body GET https://httpbin.org/uuid
# 跟随重定向
http --follow GET https://httpbin.org/redirect/1方法可省略:http https://httpbin.org/get 默认 GET。需要原始 HTTP 或下载文件时,curl 仍更合适;日常 JSON API 调试优先 http。
POST 与 JSON body
# JSON 对象:字段名=值,HTTPie 自动设 Content-Type 并序列化
http POST https://httpbin.org/post name=Ada role=admin
# 嵌套或数组:用 := 传原始 JSON 片段
http POST https://httpbin.org/post tags:='["go","rust"]'
# 从文件读 JSON
http POST https://httpbin.org/post < payload.json
# 表单字段(非 JSON API)
http --form POST https://httpbin.org/post email=test@example.com= 表示字符串字段;:= 表示 JSON 字面量。POST 到生产环境前先在 staging 或 httpbin 类沙箱验证 schema。
自定义请求头
# Header 语法:Name:Value(冒号后无空格也可)
http GET https://httpbin.org/headers Authorization:"Bearer YOUR_TOKEN" Accept:application/json
# 常见 API 头
http GET api.example.com/v1/me X-Api-Key:xxx User-Agent:my-cli/1.0
# 下载时接受任意类型
http --download GET https://example.com/file.zipHTTPie 与 curl 对照
| 目标 | HTTPie | curl |
|---|---|---|
| GET JSON | http GET https://httpbin.org/get | curl -s https://httpbin.org/get | jq . |
| 带查询参数 | http GET url q==search | curl -s 'url?q=search' |
| POST JSON | http POST url name=Ada | curl -s -X POST -H 'Content-Type: application/json' -d '{"name":"Ada"}' url |
| 自定义头 | http GET url Authorization:"Bearer x" | curl -s -H 'Authorization: Bearer x' url |
| 只看 body | http --body GET url | curl -s url |
| 上传 multipart | http --form POST url file@./a.png | curl -F file=@./a.png url |
- 适合
- 手动探 API、写 README 里的可复制示例、与 jq/rg 同一终端栈的 JSON 调试。
- 不适合
- 复杂 OAuth 设备流、精细 TLS 客户端证书、或必须在单条命令里复刻浏览器 Cookie 时——curl 或专用客户端更稳。
命令是 http 还是 httpie?
安装包名常为 httpie,可执行文件是 http(以及 https 别名)。跑 http --help 查看子命令。
如何静默输出给脚本?
用 --body 或 --print=b 只打 body;配合 jq 解析。需要 exit code 反映 HTTP 状态时加 --check-status(见官方文档)。
HTTPie 能替代 curl 吗?
日常 JSON REST 调试可以为主;curl 在旧脚本、Docker HEALTHCHECK、与任意协议细节上仍是标准。两者并存最省事。
接口对齐后画一张图
终端里理清 API 与 webhook 后,服务边界仍适合摊在同一张白板上。见 如何创建空间。