mcpField
X
xh

xh

官方Agent-ready
github.com开发者工具8.2kxh5 天前更新
代码仓库访问官网

关于 xh

xh 是一个轻量级的 HTTP 客户端,提供简洁直观的命令行语法。支持发送 GET、POST 等常见请求,兼容 HTTPie 的命令格式,可快速集成到自动化工作流中。

主要功能

  1. 01发送 HTTP 请求并查看响应
  2. 02以简洁语法指定请求头和请求体参数
  3. 03兼容 HTTPie 的命令和脚本

适用场景

  1. 01在编码 Agent 中快速测试 API 端点
  2. 02集成到自动化脚本中进行 HTTP 操作
  3. 03替代 curl 进行日常的 API 调试

安装

选择你的系统或包管理器,复制命令到终端运行。

$
brew install xh

安装完成后运行xh --version确认安装成功。

命令示例

常用命令按使用顺序排列。

01
xh GET api.github.com
02
xh POST example.com name=Codex

详细介绍

xh

版本信息 打包状态

xh 是一款友好且快速的 HTTP 请求发送工具。它尽可能多地重新实现了 HTTPie 的出色设计,并着重提升性能。

asciicast

安装

通过 cURL(Linux 和 macOS)
curl -sfL https://raw.githubusercontent.com/ducaale/xh/master/install.sh | sh
通过 Powershell(Windows)
iwr -useb https://raw.githubusercontent.com/ducaale/xh/master/install.ps1 | iex
通过包管理器
操作系统方式命令
任意Cargo*cargo install xh --locked
任意Huberhuber install xh
任意Misemise use xh --global
Android(Termux)pkgpkg install xh
Android(Magisk/KernelSU)MMRL**mmrl install xhhttp
Alpine Linuxapk***apk add xh
Arch LinuxPacmanpacman -S xh
Debian 和 UbuntuApt****sudo apt install xh
FedoraCopr*****sudo dnf install xh
FreeBSDFreshPortspkg install xh
NetBSDpkgsrcpkgin install xh
Linux 和 macOSNixpkgsnix-env -iA nixpkgs.xh
Linux 和 macOSFloxflox install xh
Linux 和 macOSHomebrewbrew install xh
Linux 和 macOSHermithermit install xh
macOSMacPortssudo port install xh
WindowsScoopscoop install xh
WindowsChocolateychoco install xh
WindowsWingetwinget add ducaale.xh

* 请确保已安装 Rust 1.85 或更高版本。

** 需要安装 MMRL CLI。

*** 仅使用 native-tls 构建。

**** 从 Debian 13 和 Ubuntu 25.04 起提供。仅使用 native-tls 构建。

***** 通过 sudo dnf copr enable ducaale/xh 启用 COPR 软件源。

通过预编译二进制文件

发布页面提供适用于 Linux、macOS 和 Windows 的预编译二进制文件。

用法

Usage: xh [OPTIONS] <[METHOD] URL> [REQUEST_ITEM]...

Arguments:
  <[METHOD] URL>     The request URL, preceded by an optional HTTP method
  [REQUEST_ITEM]...  Optional key-value pairs to be included in the request.

Options:
  -j, --json                             (default) Serialize data items from the command line as a JSON object
  -f, --form                             Serialize data items from the command line as form fields
      --multipart                        Like --form, but force a multipart/form-data request even without files
      --raw <RAW>                        Pass raw request data without extra processing
      --pretty <STYLE>                   Controls output processing [possible values: all, colors, format, none]
      --format-options <FORMAT_OPTIONS>  Set output formatting options
  -s, --style <THEME>                    Output coloring style [possible values: auto, solarized, monokai, fruity]
      --response-charset <ENCODING>      Override the response encoding for terminal display purposes
      --response-mime <MIME_TYPE>        Override the response mime type for coloring and formatting for the terminal
  -p, --print <FORMAT>                   String specifying what the output should contain
  -h, --headers                          Print only the response headers. Shortcut for --print=h
  -b, --body                             Print only the response body. Shortcut for --print=b
  -m, --meta                             Print only the response metadata. Shortcut for --print=m
  -v, --verbose...                       Print the whole request as well as the response
      --debug                            Print full error stack traces and debug log messages
      --all                              Show any intermediary requests/responses while following redirects with --follow
  -P, --history-print <FORMAT>           The same as --print but applies only to intermediary requests/responses
  -q, --quiet...                         Do not print to stdout or stderr
  -S, --stream                           Always stream the response body
  -x, --compress...                      Content compressed (encoded) with Deflate algorithm
  -o, --output <FILE>                    Save output to FILE instead of stdout
  -d, --download                         Download the body to a file instead of printing it
  -c, --continue                         Resume an interrupted download. Requires --download and --output
      --session <FILE>                   Create, or reuse and update a session
      --session-read-only <FILE>         Create or read a session without updating it from the request/response exchange
  -A, --auth-type <AUTH_TYPE>            Specify the auth mechanism [possible values: basic, bearer, digest]
  -a, --auth <USER[:PASS] | TOKEN>       Authenticate as USER with PASS (-A basic|digest) or with TOKEN (-A bearer)
      --ignore-netrc                     Do not use credentials from .netrc
      --offline                          Construct HTTP requests without sending them anywhere
      --check-status                     (default) Exit with an error status code if the server replies with an error
  -F, --follow                           Do follow redirects
      --max-redirects <NUM>              Number of redirects to follow. Only respected if --follow is used
      --timeout <SEC>                    Connection timeout of the request
      --proxy <PROTOCOL:URL>             Use a proxy for a protocol. For example: --proxy https:http://proxy.host:8080
      --verify <VERIFY>                  If "no", skip SSL verification. If a file path, use it as a CA bundle
      --cert <FILE>                      Use a client side certificate for SSL
      --cert-key <FILE>                  A private key file to use with --cert
      --ssl <VERSION>                    Force a particular TLS version [possible values: auto, tls1, tls1.1, tls1.2, tls1.3]
      --https                            Make HTTPS requests if not specified in the URL
      --http-version <VERSION>           HTTP version to use [possible values: 1.0, 1.1, 2, 2-prior-knowledge, 3-prior-knowledge]
      --resolve <HOST:ADDRESS>           Override DNS resolution for specific domain to a custom IP
      --interface <NAME>                 Bind to a network interface or local IP address
  -4, --ipv4                             Resolve hostname to ipv4 addresses only
  -6, --ipv6                             Resolve hostname to ipv6 addresses only
      --unix-socket <FILE>               Connect using a Unix domain socket
  -I, --ignore-stdin                     Do not attempt to read stdin
      --curl                             Print a translation to a curl command
      --curl-long                        Use the long versions of curl's flags
      --generate <KIND>                  Generate shell completions or man pages
      --help                             Print help
  -V, --version                          Print version

Each option can be reset with a --no-OPTION argument.

运行 xh help 或 man xh 获取更详细的信息。

请求项

xh 使用 HTTPie 的请求项语法来设置标头、请求正文、查询字符串等。

  • 使用 =/:= 设置请求正文的 JSON 或表单字段(= 用于字符串,:= 用于其他 JSON 类型)。
  • 使用 == 添加查询字符串。
  • 使用 @ 在多部分请求中包含文件,例如 [email protected] 或 [email protected];type=image/jpeg;filename=goodbye.jpg。
  • 使用 : 添加或移除标头,例如 connection:keep-alive 或 connection:。
  • 使用 ; 包含值为空的标头,例如 header-without-value;。

可以使用 @ 前缀从文件中读取值。例如:x-api-key:@api-key.txt。

请求正文也可以从标准输入读取,或使用 @filename 从文件读取。

要构造复杂的 JSON 对象,可以将 JSON 路径用作键,例如 app[container][0][id]=090-5。 有关此语法的更多信息,请参阅 https://httpie.io/docs/cli/nested-json。

URL 的简写形式

与 HTTPie 类似,请求 URL 中的协议部分可以省略,开头的冒号可作为 localhost 的简写。:8000 等同于 localhost:8000,而 :/path 等同于 localhost/path。

URL 可以以 :// 开头,从而快速将 URL 转换为有效的 xh 或 HTTPie 命令。例如,http://httpbin.org/json 会变为 http ://httpbin.org/json。

xh http://localhost:3000/users # resolves to http://localhost:3000/users
xh localhost:3000/users        # resolves to http://localhost:3000/users
xh :3000/users                 # resolves to http://localhost:3000/users
xh :/users                     # resolves to http://localhost:80/users
xh example.com                 # resolves to http://example.com
xh ://example.com              # resolves to http://example.com
默认发起 HTTPS 请求

如果二进制文件名为 xhs、https 或 xhttps,xh 默认会使用 HTTPS 协议。如果你通过包管理器安装了 xh,默认情况下应该可以使用 xh 和 xhs。否则,你需要按如下方式创建一个:

cd /path/to/xh && ln -s ./xh ./xhs
xh httpbin.org/get  # resolves to http://httpbin.org/get
xhs httpbin.org/get # resolves to https://httpbin.org/get
严格兼容模式

如果将二进制文件重命名为 http 或 https 后调用 xh,或者设置了 XH_HTTPIE_COMPAT_MODE 环境变量,它将以 HTTPie 兼容模式运行。当前唯一的区别是,默认情况下不会启用 --check-status。

示例

# Send a GET request
xh httpbin.org/json

# Send a POST request with body {"name": "ahmed", "age": 24}
xh httpbin.org/post name=ahmed age:=24

# Send a GET request with querystring id=5&sort=true
xh get httpbin.org/json id==5 sort==true

# Send a GET request and include a header named x-api-key with value 12345
xh get httpbin.org/json x-api-key:12345

# Send a POST request with body read from stdin.
echo "[1, 2, 3]" | xh post httpbin.org/post

# Send a PUT request and pipe the result to less
xh put httpbin.org/put id:=49 age:=25 | less

# Download and save to res.json
xh -d httpbin.org/json -o res.json

# Make a request with a custom user agent
xh httpbin.org/get user-agent:foobar

xh 与 HTTPie 的比较

优点
  • 启动速度更快。
  • 提供单个静态链接的二进制文件,易于安装和携带。
  • 支持 HTTP/2。
  • 使用 --curl 标志内置转换为 curl 命令的功能。
  • --help 输出简短,类似速查表。(如需更长的输出,请传入 help。)
缺点
  • 并未实现 HTTPie 的所有功能。(#4)
  • 没有插件系统。
  • 整体尚不成熟。HTTPie 历史悠久且经过充分测试。
  • 文档质量较差。

类似或相关项目

  • curlie - cURL 前端,兼具 httpie 的易用性
  • httpie-go - 使用 Go 编写的类 httpie HTTP 客户端
  • curl2httpie - 在 cURL 和 HTTPie 之间转换命令参数

评论

支持 Markdown