HTTPie 3.2.4 CLI 命令详解
HTTPie 3.2.4 核心使用篇:http/https 命令完整语法(方法、URL、请求项)、JSON/表单/文件上传、认证、会话、下载、重定向与代理逐类讲透,每条命令均配实测回显。
HTTPie 3.2.4 CLI 命令详解
目标:作为 HTTPie 3.2.4 的核心使用篇,把
http/https命令的完整语法(方法、URL、请求项)、JSON/表单/文件上传、认证、会话、下载、重定向与代理逐类讲透,每条命令均配真实服务器实测回显,可直接照抄执行。
一、环境信息
- 操作系统:Ubuntu 24.04.5 LTS(Noble Numbat),x86_64,内核 7.0.0-38-generic
- HTTPie:3.2.4(pipx 安装于
~/.local/bin/http;安装过程见《Ubuntu 24.04 安装 HTTPie 3.2.4 指南》) - Python:3.12.3(Requests 2.34.2 / Pygments 2.21.0,
http --debug实测) - 测试端点:
httpbin.org(公开 HTTP 测试服务,直连实测可达,HTTP 200 约 1.2s 首包);TLS 对照端点self-signed.badssl.com - 实验目录:
/root/httpie-demo/(本文示例文件均在此创建) - 配置状态:
~/.config/httpie/config.json为{}(无默认选项干扰,回显为纯净行为)
二、工具概览
2.1 三个入口命令
| 命令 | 作用 | 与 http 的关系 |
|---|---|---|
http | 发 HTTP/HTTPS 请求,URL 无 scheme 时默认 http:// | 主命令 |
https | 同 http,URL 无 scheme 时默认 https:// | 等价别名 |
httpie | 管理命令,承载插件管理子命令 | 不发请求 |
httpie --version
实测回显:
3.2.4
2.2 命令结构
http [METHOD] URL [REQUEST_ITEM ...]
- METHOD:可省略。省略时——请求里有 body 项(
=、:=、@、表单字段、stdin 数据等)默认POST,否则默认GET(http --help原文:"HTTPie will use POST if there is some data to be sent, otherwise GET"); - URL:支持 localhost 简写(
:3000/path→localhost:3000/path)与自动补全 scheme; - REQUEST_ITEM:键值对形式的请求部件,是 HTTPie 语法的灵魂,速查表如下。
2.3 请求项语法速查表
| 写法 | 归属 | 语义 | 实测示例 |
|---|---|---|---|
name=value | 请求体 | JSON 字符串字段 | name=alice |
name:=JSON | 请求体 | 原始 JSON(数字/布尔/null/数组/对象) | age:=30 |
name=@/path | 请求体 | 从文件读内容作为字符串字段 | msg=@note.txt |
name@/path | 请求体 | 文件上传(multipart) | file@upload.txt |
name==value | URL | 查询参数(拼接进 query string) | search==httpie |
Header:value | 请求头 | 自定义请求头 | X-Api-Token:abc123 |
Header: | 请求头 | 移除默认请求头(置空值即移除) | Referer: |
Header;value | 请求头 | 值可含特殊字符不做分隔 | — |
三、前置条件
mkdir -p /root/httpie-demo && cd /root/httpie-demo
http --version
实测回显:
3.2.4
⚠️ 重要前置(脚本场景):本文所有命令在交互终端与脚本中行为一致,因为统一带了
--ignore-stdin。在脚本/管道里裸跑http时(stdin 非终端),HTTPie 会读取 stdin 当作请求体,导致无 body 项的请求默认方法变成 POST——排障实录见 4.2。
四、基础请求
4.1 最简 GET
http --ignore-stdin https://httpbin.org/get
实测回显:
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "HTTPie/3.2.4",
"X-Amzn-Trace-Id": "Root=1-6add7ad8-6e9bc9052b6160fb0700ba81"
},
"origin": "182.54.113.29",
"url": "https://httpbin.org/get"
}
💡 说明:默认输出=格式化缩进的响应体(
--print=hb中的b;请求头在终端场景不显示)。JSON 中的origin是本服务器出口 IP。若想连请求报文一起看,加-v(见 10.1)。
4.2 默认方法规则与 --ignore-stdin 排障实录
这是 HTTPie 脚本使用率最高的问题,本机完整复现如下:
错误现象:脚本中执行 http httpbin.org/get(期望 GET),实际发出 POST:
http --offline httpbin.org/get
实测回显(--offline 只构造打印请求、不发送,用于看真实方法):
POST /get HTTP/1.1
Accept-Encoding: gzip, deflate
Connection: keep-alive
定位:执行环境(脚本/CI/cron)的 stdin 不是终端(TTY)。HTTPie 启动时检测到非 TTY stdin 会读取它作为潜在请求体——即使 stdin 为空,也让"有数据待发"成立,默认方法因此变成 POST。交互终端不存在此问题。
解决方案:
# 方案一(官方推荐):显式加 --ignore-stdin,恢复标准语义
http --ignore-stdin --offline httpbin.org/get
实测回显:
GET /get HTTP/1.1
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive
User-Agent: HTTPie/3.2.4
Host: httpbin.org
# 方案二:显式写方法
http GET httpbin.org/get --offline
实测回显(输出节选,首行):
GET /get HTTP/1.1
⚠️ 结论:脚本、CI、cron、管道场景一律加
--ignore-stdin;配合 config.json 的default_options可全局固化(见《HTTPie 3.2.4 配置参数详解》篇生产配置模板)。
4.3 查询参数:==
http --ignore-stdin --print=Hb https://httpbin.org/get search==httpie limit==5
实测回显:
GET /get?search=httpie&limit=5 HTTP/1.1
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive
User-Agent: HTTPie/3.2.4
Host: httpbin.org
{
"args": {
"limit": "5",
"search": "httpie"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "HTTPie/3.2.4",
"X-Amzn-Trace-Id": "Root=1-6add7ad9-7d3f2d2915ee585f113ed26c"
},
"origin": "182.54.113.29",
"url": "https://httpbin.org/get?search=httpie&limit=5"
}
💡 说明:
==项自动 URL 编码并拼进 query string,无需手工引号转义(含空格/中文时同样安全)。--print=Hb大写 H 表示打印请求头,便于确认最终请求行。
4.4 localhost 简写
# :3000/api/users 等价于 http://localhost:3000/api/users
http --ignore-stdin --offline :3000/api/users id:=7
实测回显:
POST /api/users HTTP/1.1
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 9
User-Agent: HTTPie/3.2.4
Accept: application/json, */*;q=0.5
Content-Type: application/json
Host: localhost:3000
{"id": 7}
💡 说明:请求体有 JSON 项(
id:=7)故默认 POST;--offline下无需真实服务监听 3000 端口即可验证请求构造。裸:3000表示localhost:3000/。
五、请求体构造
5.1 JSON 字段类型语法
默认 Content-Type 为 application/json,= / := 决定值类型:
http --ignore-stdin --print=HB httpbin.org/post \
name=alice \
age:=30 \
married:=true \
discount:=1.5 \
tags:='["a","b"]' \
address:='{"city":"Beijing"}'
实测回显:
POST /post HTTP/1.1
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 136
User-Agent: HTTPie/3.2.4
Accept: application/json, */*;q=0.5
Content-Type: application/json
Host: httpbin.org
{"name": "alice", "age": 30, "married": true, "tags": ["a", "b"], "address": {"city": "Beijing"}, "discount": 1.5}
类型对照(同一输出逐字段核对):
| 写法 | 发送的 JSON | 说明 |
|---|---|---|
name=alice | "name": "alice" | = 恒为字符串 |
age:=30 | "age": 30 | 数字 |
married:=true | "married": true | 布尔 |
discount:=1.5 | "discount": 1.5 | 浮点 |
tags:='["a","b"]' | "tags": ["a", "b"] | 数组 |
address:='{"city":"Beijing"}' | "address": {"city": "Beijing"} | 嵌套对象 |
note:=null | "note": null | null 字面量 |
# null 字面量的正确写法是 :=null
http --ignore-stdin --offline httpbin.org/post note:=null next_id:=null
实测回显(输出节选,响应体行):
{"note": null, "next_id": null}
⚠️ 注意:实测踩过的写法坑——
note=null:='null'这类把:=混在值里的写法会被整体当作字符串(实测发送"note": "null:=null")。=与:=是两种独立写法,一个键只选其一。
5.2 自定义请求头::
http --ignore-stdin --print=Hh httpbin.org/get X-Api-Token:abc123 Referer:https://example.com/
实测回显:
GET /get HTTP/1.1
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive
User-Agent: HTTPie/3.2.4
X-Api-Token: abc123
Referer: https://example.com/
Host: httpbin.org
HTTP/1.1 200 OK
Date: Sun, 25 Oct 2026 03:44:59 GMT
Content-Type: application/json
5.3 表单:-f
http --ignore-stdin --print=HB -f httpbin.org/post username=root password=q1w2e3r4
实测回显:
POST /post HTTP/1.1
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive
Content-Length: 31
User-Agent: HTTPie/3.2.4
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Host: httpbin.org
username=root&password=q1w2e3r4
💡 说明:
-f后所有=字段变为表单编码,Content-Type 自动切换为application/x-www-form-urlencoded;需要multipart/form-data(如带文件)时用--multipart或直接加@文件项(HTTPie 检测到文件上传自动切换 multipart)。
5.4 文件上传:@(multipart 实测)
cd /root/httpie-demo
echo "hello httpie upload" > upload.txt
http --ignore-stdin --print=HB -f httpbin.org/post file@upload.txt remark=from-httpie
实测回显:
POST /post HTTP/1.1
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive
Content-Length: 290
User-Agent: HTTPie/3.2.4
Content-Type: multipart/form-data; boundary=c741cd49225446259c07ada3c766e250
Host: httpbin.org
--c741cd49225446259c07ada3c766e250
Content-Disposition: form-data; name="file"; filename="upload.txt"
Content-Type: text/plain
hello httpie upload
--c741cd49225446259c07ada3c766e250
Content-Disposition: form-data; name="remark"
from-httpie
--c741cd49225446259c07ada3c766e250--
💡 说明:
file@upload.txt(单@)是文件字段:字段名file、文件名与 Content-Type 按文件自动推断;msg=@note.txt(=后接@)则是把文件内容读成字符串当普通字段——一字之差两种语义,是文件上传最常见的混淆点。
5.5 原始请求体:--raw 与 stdin 管道
# --raw 直接给原始 body,不做键值解析
http --ignore-stdin --raw '{"a":1,"b":[2,3]}' --print=HB httpbin.org/post
实测回显(输出节选,请求体行):
{"a":1,"b":[2,3]}
# stdin 作为请求体(管道场景)
echo '{"from":"stdin","ok":true}' | http --print=HB httpbin.org/post
实测回显(输出节选,请求体行):
{"from":"stdin","ok":true}
⚠️ 注意:管道场景不要加
--ignore-stdin(那会丢弃 stdin 数据);此时 stdin 数据本身就是请求体,默认方法为 POST 是预期行为。交互场景不想读 stdin 才加--ignore-stdin。
六、认证
6.1 Basic 认证(-a)
http --ignore-stdin --print=Hh -a admin:q1w2e3r4 https://httpbin.org/basic-auth/admin/q1w2e3r4
实测回显:
GET /basic-auth/admin/q1w2e3r4 HTTP/1.1
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive
Authorization: Basic YWRtaW46cTF3MmUzcjQ=
User-Agent: HTTPie/3.2.4
Host: httpbin.org
HTTP/1.1 200 OK
Date: Sun, 25 Oct 2026 03:44:22 GMT
Content-Type: application/json
Content-Length: 48
💡 说明:
Authorization: Basic YWRtaW46cTF3MmUzcjQ=即admin:q1w2e3r4的 Base64(echo -n 'admin:q1w2e3r4' | base64实测输出一致)。-a 用户:密码默认就是 basic,等价-A basic -a ...。
6.2 认证失败与 --check-status 退出码
# 密码错误:401 + 空 body(httpbin 该端点行为)
http --ignore-stdin --print=hb -a admin:wrongpass https://httpbin.org/basic-auth/admin/q1w2e3r4
实测回显:
HTTP/1.1 401 UNAUTHORIZED
Date: Sun, 25 Oct 2026 03:44:44 GMT
Content-Length: 0
Connection: keep-alive
Server: gunicorn/19.9.0
WWW-Authenticate: Basic realm="Fake Realm"
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
# 加 --check-status 后失败映射为退出码,供脚本判断
http --ignore-stdin --check-status -a admin:wrongpass https://httpbin.org/basic-auth/admin/q1w2e3r4 > /dev/null 2>&1
echo "exit=$?"
实测回显:
exit=4
退出码体系(httpbin.org/status/<code> 端点逐档实测):
| HTTP 结果 | 退出码 | 实测命令与回显 |
|---|---|---|
| 2xx | 0 | http --check-status httpbin.org/status/200 → exit=0 |
| 3xx(未跟随) | 3 | …/redirect/1 → exit=3,stderr:http: warning: HTTP 302 FOUND |
| 4xx | 4 | …/status/404 → exit=4 |
| 5xx | 5 | …/status/500 → exit=5 |
| 连接层错误 | 2 | 超时/拒绝连接等 → exit=2 |
6.3 Digest 认证(--auth-type digest)
http --ignore-stdin --auth-type digest -a admin:q1w2e3r4 https://httpbin.org/digest-auth/auth/admin/q1w2e3r4
实测回显:
{
"authenticated": true,
"user": "admin"
}
💡 技术拓展:HTTP 认证方案不止 HTTPie 内置的三种。常见方案对比——
方案 原理/特点 适用场景 Basic 用户:密码 Base64 明文随请求发送 内网/HTTPS 下的简单接口 Digest 服务端挑战 + MD5 摘要应答,不传明文 老系统,现代场景已少用 Bearer 携带令牌(JWT/OAuth2 access token) 现代 API 主流 JWT 插件 -A jwt经插件签名,支持 secret/算法头面向 JWT 服务的调试 HMAC 签名 请求级签名防篡改 开放平台 API(如云厂商) HTTPie 3.2.4 内置 basic/digest/bearer(
http --help实测值域),其余经插件扩展(安装实测见《HTTPie 3.2.4 配置参数详解》篇 5.6)。Bearer 用法:http -A bearer -a <token> <URL>。
七、会话
# 第一条:登录并把认证写入会话 docs-demo
http --ignore-stdin --session=docs-demo -a admin:q1w2e3r4 https://httpbin.org/basic-auth/admin/q1w2e3r4 > /dev/null
# 第二条:不带任何凭据,Authorization 自动从会话注入
http --ignore-stdin --session=docs-demo --print=Hh https://httpbin.org/get
实测回显(第二条命令,输出节选):
GET /get HTTP/1.1
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive
Authorization: Basic YWRtaW46cTF3MmUzcjQ=
User-Agent: HTTPie/3.2.4
Host: httpbin.org
HTTP/1.1 200 OK
Date: Sun, 25 Oct 2026 03:44:59 GMT
Content-Type: application/json
Content-Length: 349
会话文件落盘位置与结构(cat ~/.config/httpie/sessions/httpbin.org/docs-demo.json 实测):
{
"__meta__": {
"about": "HTTPie session file",
"help": "https://httpie.io/docs#sessions",
"httpie": "3.2.4"
},
"auth": {
"raw_auth": "admin:q1w2e3r4",
"type": "basic"
},
"cookies": [],
"headers": []
}
💡 说明:会话按 主机名 分目录(
sessions/httpbin.org/),Cookie、自定义头、认证三项随请求自动累积;凭据明文存储的权限建议与逐键解析见《HTTPie 3.2.4 配置参数详解》篇 5.5。只读复用(不写回)用--session-read-only。
八、上传与下载
8.1 下载:--download
cd /root/httpie-demo
http --ignore-stdin --download https://httpbin.org/bytes/2048 --output sample.bin
ls -la sample.bin
实测回显:
Downloading to sample.bin
Done. 2.0 kB in 00:0.02416 (84.8 kB/s)
-rw-r--r-- 1 root root 2048 Oct 25 11:45 sample.bin
💡 说明:
--download(-d)把响应流式落盘并显示进度(大文件时实时刷新),--output指定文件名;断点续传加--continue`(服务端需支持 Range)。响应头仍正常打印,body 不再进终端。
8.2 上传见 5.4(multipart 实测)
九、重定向与代理
9.1 跟随重定向:--follow + --all + --history-print
# 默认只显示最终响应
http --ignore-stdin --follow --print=h httpbin.org/redirect/2
实测回显:
HTTP/1.1 200 OK
Date: Sun, 25 Oct 2026 03:46:00 GMT
Content-Type: application/json
Content-Length: 296
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
# --all 显示中间每一跳(--history-print 指定中间请求/响应的打印范围)
http --ignore-stdin --follow --all --print=h --history-print=h httpbin.org/redirect/2
实测回显(只保留状态行与 Location,便于观察跳转链):
HTTP/1.1 302 FOUND
Location: /relative-redirect/1
HTTP/1.1 302 FOUND
Location: /get
HTTP/1.1 200 OK
💡 说明:
--follow(-F)才跟随 3xx;默认最多 30 跳(--max-redirects=N可调)。调试认证跳转/网关 302 时,--all一屏看全每一跳的请求与响应。
9.2 代理:--proxy
# 格式 PROTOCOL:PROXY_URL,http 与 https 分开声明
http --ignore-stdin --proxy=http:http://192.168.1.62:7897 --print=m httpbin.org/get
实测回显(经代理成功,只看耗时元信息):
Elapsed time: 0.443759131s
# 代理不可达时的真实报错(实测)
http --ignore-stdin --proxy=http:http://182.54.113.29:7897 httpbin.org/get
实测回显(输出节选):
http: error: ProxyError: HTTPConnectionPool(host='182.54.113.29', port=7897): Max retries exceeded with url: http://httpbin.org/get (Caused by ProxyError('Unable to connect to proxy', NewConnectionError("HTTPConnection(host='182.54.113.29', port=7897): Failed to establish a new connection: [Errno 111] Connection refused"))) while doing a GET request to URL: http://httpbin.org/get
💡 说明:
--proxy支持http/https/all三种协议前缀(--proxy=all:...一条管全部)。已配http_proxy/https_proxy环境变量的服务器,--proxy显式指定优先。
十、输出控制
10.1 --verbose:请求+响应全量
http --ignore-stdin --verbose POST httpbin.org/post name=alice
实测回显(输出节选):
POST /post HTTP/1.1
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 17
User-Agent: HTTPie/3.2.4
Accept: application/json, */*;q=0.5
Content-Type: application/json
Host: httpbin.org
{"name": "alice"}
HTTP/1.1 200 OK
Date: Sun, 25 Oct 2026 03:47:14 GMT
Content-Type: application/json
💡 说明:
-v等价--print=HBhb(请求头+请求体+响应头+响应体全打),是排查"到底发了什么、回了什么"的日常工具。
10.2 --print 取值与非法值报错
--print 只接受 H B h b m 五个字符的组合(H/B 请求侧,h/b 响应侧,m 元信息含耗时)。写非法字符时实测报错:
http --ignore-stdin --print=f httpbin.org/get
实测回显:
usage:
http [METHOD] URL [REQUEST_ITEM ...]
error:
Unknown output options: --print=f
for more information:
run 'http --help' or visit https://httpie.io/docs/cli
# -h / -b 快捷方式与耗时元信息
http --ignore-stdin --print=m httpbin.org/get
实测回显:
Elapsed time: 0.49302511s
10.3 --style 配色与 --pretty
# --style 可选值实测(故意写非法值,报错即全量清单)
http --ignore-stdin --style=no-such-style httpbin.org/get
实测回显(输出节选,报错同时列出全部合法值):
usage:
http -s/--style {abap, algol, algol_nu, arduino, auto, autumn, borland, bw,
coffee, colorful, default, dracula, emacs, friendly, friendly_grayscale, fruity,
💡 说明:默认
--style=auto(按终端能力选)。--pretty {all,colors,format,none}控制着色/格式化开关,检测到输出重定向时 HTTPie 自动降级为none(管道进 jq/文件时无需手工关色)。--style与--pretty均可写入 config.json 的default_options固化(见《HTTPie 3.2.4 配置参数详解》篇)。
10.4 落盘:--output
http --ignore-stdin --output body.json httpbin.org/get
head -5 body.json
实测回显:
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
十一、注意事项
- 脚本必加
--ignore-stdin(4.2 排障实录):stdin 非 TTY 会改变默认方法;交互终端不受影响。 - 退出码三态要分清:
--check-status才把 3xx/4xx/5xx 映射为 3/4/5;不加时 HTTP 正常返回一律退出码 0(连接层错误仍为 2)——CI 判断失败必须加--check-status。 - 凭据与会话明文:
-a会出现在 shell history;会话文件sessions/<host>/<name>.json明文存凭据——共享服务器建议chmod 600并定期清理(rm -rf ~/.config/httpie/sessions)。 =与:=别混写:一个键只用一种写法,混写(note=null:='null')实测被当字符串(5.1 实测踩坑)。- 文件上传一字之差:
f@path是文件字段(multipart),f=@path是读文件内容为字符串字段(5.4 实测)。 - 代理与 TLS:内网自签名证书用
--verify=no临时放行或--verify=/path/ca.pem信任私有 CA(实测对照见《HTTPie 3.2.4 配置参数详解》篇 5.4)。 - 无超时默认:不设
--timeout时请求可无限等待,CI 场景务必显式设超时。
十二、常见问题(FAQ)
Q1:命令行太长怎么换行?
shell 续行符 \(5.1 示例即多行写法);或把常用选项固化到 config.json 的 default_options。
Q2:想看发送的原始报文但不想真的发请求?
--offline(4.2/4.4 实测),只构造打印不发送,是语法学习与 CI 自检利器。
Q3:响应是 GBK/Big5 乱码?
--response-charset=big5 强制解码字符集(http --help 实测值示例含 utf8/big5)。
Q4:怎么发送 DELETE/PUT/PATCH?
方法直接写:http DELETE https://api.example.com/items/7、http PUT ... name=new;有 body 项时 PUT/PATCH 同样自动带 JSON。
Q5:流式接口(SSE/chunked)想边收边看?
--stream(-S)禁用缓冲即时打印每一块;配合 --pretty=colors 观感最好。
Q6:http 与 curl 输出不一样,如何拿到完全一致的原始字节?
--pretty=none 关闭全部格式化(响应原始字节),或 -o 落盘后比对;HTTPie 的格式化只影响终端展示,不影响 --output 文件内容的原始性(--pretty=none 实测输出为未着色 JSON)。
十三、参考链接
- HTTPie 官方文档·Usage:https://httpie.io/docs/cli/usage (查询日期 2026-10-25)
- HTTPie 官方文档·JSON:https://httpie.io/docs/cli/json (查询日期 2026-10-25)
- HTTPie 官方文档·Auth:https://httpie.io/docs/cli/authentication (查询日期 2026-10-25)
- HTTPie 官方文档·Sessions:https://httpie.io/docs/cli/sessions (查询日期 2026-10-25)
- httpbin.org 测试服务:https://httpbin.org (查询日期 2026-10-25)
- 本系列配置详解:《HTTPie 3.2.4 配置参数详解》
- 本系列安装实测:《Ubuntu 24.04 安装 HTTPie 3.2.4 指南》
- 本系列选型认知:《HTTPie 3.2.4 介绍与选型指南》