xh - 用于发送 HTTP 请求的友好且快速的工具

Created at: 2020-09-11 04:58:13
Language: Rust
License: MIT

xh

版本信息 包装状态

xh
是一个友好且快速的发送 HTTP 请求的工具。它尽可能多地重新实现了HTTPie 的优秀设计,重点是提高了性能。

asciicast

安装

通过 cURL(Linux 和 macOS)

curl -sfL https://raw.githubusercontent.com/ducaale/xh/master/install.sh | sh

通过包管理器

操作系统 方法 命令
任何 货物*
cargo install xh
任何 胡贝尔
huber install xh
拱形Linux 吃豆子
pacman -S xh
Debian 和 Ubuntu 易于**
sudo apt install xh
FreeBSD 新鲜 Port
pkg install xh
Linux 和 macOS 尼克斯
nix-env -iA nixpkgs.xh
Linux 和 macOS 家酿
brew install xh
苹果系统 端口
sudo port install xh
视窗
scoop install xh

* 确保你安装了 Rust 1.46 或更高版本

** 你需要从https://apt.cli.rs/添加 apt 存储库

通过预先构建的二进制文件

版本页面包含适用于Linux,MacOS和的Windows预编译的二进制。

用法

USAGE:
    xh [OPTIONS] <[METHOD] URL> [--] [REQUEST_ITEM]...

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
    -m, --multipart                      Like --form, but force a multipart/form-data request even without files
        --pretty <STYLE>                 Controls output processing [possible values: all, colors, format, none]
    -s, --style <THEME>                  Output coloring style [possible values: auto, solarized, monokai]
        --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
    -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 or with TOKEN
        --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 set
        --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, ssl2.3, tls1, tls1.1, tls1.2, tls1.3]
        --native-tls                     Use the system TLS library instead of rustls (if enabled at compile time)
        --https                          Make HTTPS requests if not specified in the URL
        --http-version <VERSION>         HTTP version to use [possible values: 1, 1.0, 1.1, 2]
    -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                           Prints help information
    -V, --version                        Prints version information

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

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

运行

xh help
以获取更多详细信息。

请求项目

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

  • =
    /
    :=
    用于设置请求正文的 JSON 或表单字段(
    =
    用于字符串和
    :=
    其他 JSON 类型)。
  • ==
    用于添加查询字符串。
  • @
    用于在多部分请求中包含文件,例如
    picture@hello.jpg
    picture@hello.jpg;type=image/jpeg;filename=goodbye.jpg
  • :
    用于添加或删除标题,例如
    connection:keep-alive
    connection:
  • ;
    用于包含具有空值的标题,例如
    header-without-value;
    .
  • =@
    /
    :=@
    用于设置请求正文的 JSON 或来自文件的表单字段(
    =@
    用于字符串和
    :=@
    其他 JSON 类型)。

请求正文也可以从标准输入或使用

@filename
.

URL 的简写形式

与 HTTPie 类似,指定请求 URL 的方案部分是可选的。

xh
还支持
localhost
从 URL 中省略,只要它以冒号开头加上一个可选的端口号。

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

默认情况下发出 HTTPS 请求

xh
将默认使用HTTPS方案如果二进制名称是一个
xhs
https
xhttps
。如果你是
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 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

xh 与 HTTPie 的比较

好处

  • 提高了启动速度。
  • 作为一个单一的静态链接二进制文件提供,易于安装和携带。
  • HTTP/2 支持。
  • 带有
    --curl
    标志的curl 命令的内置翻译。
  • 来自
    --help
    . (对于更长的输出,通过
    help
    。)

缺点

  • 并非 HTTPie 的所有功能都已实现。( #4 )
  • 标题名称不区分大小写。
  • 没有插件系统。
  • 一般不成熟。HTTPie 已经过时且经过良好测试。
  • 更糟糕的文档。

其他差异

  • --check-status
    除非
    xh
    严格兼容模式下使用, 否则启用。
  • rustls
    默认使用 TLS 库而不是系统的 TLS 库。(如果在编译时启用,则
    --native-tls
    可以使用该标志。)
  • JSON 键未排序。
  • 格式化输出始终为 UTF-8。

类似或相关项目

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