24 个版本 (15 个破坏性更新)
0.22.2 | 2024 年 7 月 8 日 |
---|---|
0.21.0 | 2024 年 1 月 28 日 |
0.20.1 | 2023 年 11 月 19 日 |
0.18.0 | 2023 年 2 月 20 日 |
0.9.2 | 2021 年 3 月 24 日 |
#17 在 HTTP 客户端 中
每月 475 次下载
750KB
6.5K SLoC
xh
xh
是一个友好且快速的 HTTP 请求工具。它尽可能地实现了 HTTPie 的优秀设计,并着重于提高性能。
安装
通过 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* | cargoinstall xh --锁定 |
任何 | Huber | huber install xh |
Android (Termux) | pkg | pkg install xh |
Android (Magisk/KernelSU) | MMRL** | mmrl install xhhttp |
Alpine Linux | apk*** | apk add xh |
Arch Linux | Pacman | pacman -S xh |
Debian & Ubuntu | Apt**** | sudoapt install xh |
FreeBSD | FreshPorts | pkg install xh |
NetBSD | pkgsrc | pkgin install xh |
Linux & macOS | Nixpkgs | nix-env-iA nixpkgs.xh |
Linux & macOS | Homebrew | brewinstall xh |
macOS | MacPorts | sudoport install xh |
Windows | Scoop | scoop install xh |
Windows | Chocolatey | choco install xh |
Windows | Winget | winget add ducaale.xh |
* 确保已安装 Rust 1.74 或更高版本
** 需要安装 MMRL CLI
*** xh 软件包在 Edge 中可用,将在 v3.17+ 中提供。它仅使用 native-tls 构建。
**** 需要添加来自 https://apt.cli.rs/ 的 apt 仓库
通过预构建的二进制文件
发布页面包含了适用于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
--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
-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 form 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]
--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
-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
--help Print help
-V, --version Print version
Each option can be reset with a --no-OPTION argument.
运行 xh help
以获取更多信息。
请求项
xh
使用 HTTPie 的请求项语法 来设置头部、请求体、查询字符串等。
=
/:=
用于设置请求体的 JSON 或表单字段(=
用于字符串,:=
用于其他 JSON 类型)。==
用于添加查询字符串。@
用于在multipart请求中包含文件,例如picture@hello.jpg
或picture@hello.jpg;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 发送请求
xh
如果二进制文件名为xhs
、https
或xhttps
之一,将默认使用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
严格兼容模式
如果xh
以http
或https
(通过重命名二进制文件)调用,或者如果设置了环境变量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
提供简短、类似cheatsheet的输出。(对于更长的输出,请传递help
。)
劣势
- 并非所有HTTPie的功能都已实现。(#4)
- 没有插件系统。
- 总体不成熟。HTTPie已存在并经过充分测试。
- 较差的文档。
类似或相关项目
- curlie - cURL的前端,增加了httpie的易用性
- httpie-go - 用Go编写的类似httpie的HTTP客户端
- curl2httpie - 在cURL和HTTPie之间转换命令参数
依赖项
~26–46MB
~1M SLoC