如何安裝 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 的等价写法。

安裝brew · pip · apt
请求GET · POST · JSON
對照http vs 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

  1. 1
    macOS(Homebrew)

    brew install httpie。驗證:which http 与 http --version。

  2. 2
    Linux(发行版包或 pip)

    Debian/Ubuntu:sudo apt update && sudo apt install httpie。Fedora:sudo dnf install httpie。通用:python3 -m pip install --user httpie,确保 ~/.local/bin 在 PATH。

  3. 3
    Windows

    PowerShell:winget install httpie.httpie,或 pip install httpie。新开终端后 http --version。

  4. 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.zip

HTTPie 与 curl 對照

目标HTTPiecurl
GET JSONhttp GET https://httpbin.org/getcurl -s https://httpbin.org/get | jq .
带查詢參數http GET url q==searchcurl -s 'url?q=search'
POST JSONhttp POST url name=Adacurl -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
只看 bodyhttp --body GET urlcurl -s url
上传 multiparthttp --form POST url file@./a.pngcurl -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 后,服务边界仍適合摊在同一张白板上。见 如何建立空間。

立即開會