字节笔记本

2026年2月22日

HTTPie:开发者友好的命令行 HTTP 客户端

HTTPie 是一个对开发者友好的命令行 HTTP 客户端,相比 curl 拥有更直观的语法和更可读的输出,非常适合 API 调试和开发阶段的快速测试。

基础请求功能

HTTPie 支持所有标准 HTTP 方法,语法比 curl 简洁很多:

bash
# GET 请求
http GET httpbin.org/json

# POST 请求,自动序列化为 JSON
http POST api.example.com/users name=Alice age:=30

支持的 HTTP 方法包括:GET、POST、PUT、PATCH、DELETE、HEAD、OPTIONS 等。

请求构建

JSON 数据

默认将键值对序列化为 JSON,无需手动指定 Content-Type:

bash
http POST api.example.com/users \\
  name="John Doe" \\
  email="john@example.com" \\
  age:=25 \\
  active:=true

注意:= 表示字符串,:= 表示其他类型(数字、布尔值等)。

表单数据

使用 == 发送 form-encoded 数据:

bash
http -f POST api.example.com/form \\
  name=="John Doe" \\
  message=="Hello World"

文件上传

支持 multipart 文件上传:

bash
http -f POST api.example.com/upload \\
  file@~/Documents/report.pdf \\
  description="Monthly report"

自定义 Headers

bash
http GET api.example.com/protected \\
  Authorization:"Bearer token123" \\
  X-Request-ID:uuid-1234

Query 参数

bash
http GET api.example.com/search \\
  q=="python tutorial" \\
  limit==10 \\
  sort=="date"

Raw Body

支持从文件读取请求体:

bash
http POST api.example.com/webhook < payload.json

认证支持

Basic Auth

bash
http --auth user:pass GET api.example.com/private

Bearer Token

bash
http --auth-type bearer --auth TOKEN GET api.example.com/me

Digest Auth

bash
http --auth-type digest --auth user:pass GET api.example.com/protected

插件扩展

支持插件扩展其他认证方式,如 AWS Signature、OAuth 等。

Session 管理

使用 --session 持久化 cookies 和 headers,方便模拟登录状态的连续请求:

bash
# 创建 session 并登录
http --session=logged-in POST api.example.com/login username=user password=pass

# 后续请求自动携带 session
http --session=logged-in GET api.example.com/profile
http --session=logged-in GET api.example.com/orders

HTTPS 与代理

SSL 证书验证

bash
# 禁用证书验证(开发环境)
http --verify=no GET https://self-signed.example.com

# 使用自定义 CA 证书
http --cert=/path/to/ca.crt GET https://internal.example.com

代理设置

bash
# HTTP 代理
http --proxy=http:http://proxy.example.com:8080 GET httpbin.org/ip

# HTTPS 代理
http --proxy=https:http://proxy.example.com:8080 GET https://api.example.com

# SOCKS 代理
http --proxy=all:socks5://localhost:1080 GET httpbin.org/ip

输出与调试

彩色高亮显示

HTTPie 默认彩色高亮显示请求和响应,包括 headers 和 body,大幅提升可读性。

格式化输出

JSON 响应自动美化格式化,支持语法高亮。

调试选项

bash
# 打印请求和响应详情
http --verbose GET api.example.com/users

# 只打印请求(不发送)
http --offline POST api.example.com/users name=test

# 精细控制输出内容
http --print=HhBb GET api.example.com/users
# H=请求头, h=响应头, B=请求体, b=响应体

下载功能

bash
# 下载文件
http --download GET example.com/file.zip

# 断点续传
http --download --continue GET example.com/large-file.zip

WebSocket 支持

从 3.x 版本起支持 WebSocket:

bash
wss ws://echo.websocket.org
wss wss://ws.example.com/socket

插件生态

HTTPie 支持插件扩展,社区提供多种实用插件:

  • httpie-aws-authv4:AWS Signature v4 认证
  • httpie-oauth1:OAuth 1.0a 认证
  • httpie-jwt-auth:JWT 认证

安装插件:

bash
pip install httpie-aws-authv4

Desktop / Web 版本

除 CLI 外,HTTPie 还提供图形界面版本:

  • 桌面版:跨平台 GUI 应用
  • Web 版:浏览器中运行,类似 Postman

支持团队协作、集合管理、环境变量等功能。

注意:CLI 版本完全开源,桌面版和 Web 版是闭源商业产品。

与 curl 对比

场景curlHTTPie
简单 GETcurl httpbin.org/jsonhttp GET httpbin.org/json
POST JSONcurl -X POST -H "Content-Type: application/json" -d '{"name":"test"}'http POST api.com name=test
可读性原始输出彩色格式化
调试-v 参数--verbose,更直观

安装方法

macOS

bash
brew install httpie

Linux

bash
apt install httpie        # Debian/Ubuntu
pacman -S httpie          # Arch

Python pip

bash
pip install httpie

总结

HTTPie 的核心优势在于:

  • 语法直观:键值对自动处理,无需手动构建请求
  • 输出可读:彩色高亮、自动格式化
  • 功能完整:认证、Session、代理、文件上传一应俱全
  • 开源免费:CLI 版本基于 BSD 许可证完全开源

对于日常 API 调试和开发测试,HTTPie 是 curl 的高可读性替代方案,能显著提升工作效率。

分享: