装了 xh 之后,这篇文章带你快速上手。覆盖 GET/POST/Header/Session/Download/curl 翻译这些常见场景,看完就能用在日常工作中。nn


nn

基础请求

nn

GET —— 最简单的开始

nn

Bash
xh httpbin.org/getn

nn彩色 JSON 秒回,连 -X GET 都不用写。nn

自动判断 Method

nn

Bash
# 裸 URL 就是 GETnxh https://httpbin.org/jsonnn# 带 URL 参数nxh https://httpbin.org/get name==Tom page==1n

nn

POST —— JSON Body

nn

Bash
xh post httpbin.org/post name=Tom age:=26n

nnname=Tom(字符串)和 age:=26(数字,:= 强制 JSON 类型)的区别要注意。nn

PUT / PATCH / DELETE

nn

Bash
xh put httpbin.org/put name=Updatednxh patch httpbin.org/patch name=Patchednxh delete httpbin.org/deleten

nn


nn

Header 和 Query 参数

nn

自定义 Header

nn

Bash
# 单个 Headernxh httpbin.org/get user-agent:MyApp/1.0nn# 多个 Headernxh httpbin.org/get n  accept:application/json n  x-api-key:secret123n

nnHeader 名后面跟冒号 :,空值就写 content-type:(不带值)。nn

URL Query 参数(不用手写 ? 和 &)

nn

Bash
# 代替 https://httpbin.org/get?page=2&sort=descnxh get httpbin.org/get page==2 sort==descn

nn这个设计比 curl 舒服太多。nn


nn

Form 提交和文件上传

nn

Form 表单(application/x-www-form-urlencoded)

nn

Bash
xh -f post httpbin.org/post username=admin password=123456n

nn-f--form 缩写。nn

上传文件

nn

Bash
# @ 读取文件内容nxh post httpbin.org/post document@report.pdfn

nn


nn

Session 保持登录状态

nn

Bash
# 第一次登录(Session 会保存 Cookie)nxh post :8080/login username=admin password=secret --session=loginnn# 后续请求复用同一个 Sessionnxh get :8080/api/profile --session=loginn

nn相当于浏览器里的”记住登录态”,不用手动抓 Cookie。nn


nn

下载文件和输出控制

nn

下载到文件

nn

Bash
xh -d https://httpbin.org/json -o response.jsonn

nn-d--download-o--output。nn

只看响应体或只看 Header

nn

Bash
# 只看 bodynxh httpbin.org/get -bnn# 只看 headernxh httpbin.org/get -hnn# 自定义输出格式nxh httpbin.org/get -p hmn# m = metadata, h = headers, b = bodyn

nn


nn

最实用功能:–curl 翻译

nn把 xh 命令翻译成 curl,复制给用 curl 的同事:nn

Bash
# 看翻译结果nxh post httpbin.org/post name=Tom age:=26 --curln

nn输出:nn

Bash
curl -X POST https://httpbin.org/post n  -H "Content-Type: application/json" n  -d '{"name":"Tom","age":26}'n

nn或者直接打印出来不发送(--offline):nn

Bash
xh post :8080/api/users name=admin --offline --curln

nn


nn

SSL 和超时控制

nn

跳过 SSL 验证(测试环境)

nn

Bash
xh https://expired-cert.example.com/get --verify=non

nn

超时设置

nn

Bash
xh httpbin.org/delay/5 --timeout=3n

nn3 秒超时,耐心有限的你值得拥有。nn


nnxh Session 保持登录状态nn

速查表

nn

n n n

n

n

n

n

n n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

场景 命令
GET xh url
POST JSON xh post url k=v
URL 参数 xh url k==v
Header xh url name:value
Form xh -f post url k=v
下载 xh -d url -o file
Session xh url --session=sess.txt
翻译 curl xh url --curl
离线构造 xh url --offline
跳过 SSL xh url --verify=no

nn


nn

下一步

nn安装和用法都介绍完了。英文版的使用指南也在博客上,可以转发给国外同事或 Linux 爱好者。nn有问题欢迎评论。

最后修改: 2026年7月6日

作者