xh について
xh は、コマンドライン作業用に設計された軽量な HTTP クライアントです。GET、POST などの HTTP リクエストに対して分かりやすい構文を提供し、HTTPie のコマンド形式との互換性を保ちながら、自動化スクリプトにシームレスに統合できます。
主な機能
- 01HTTP リクエストを送信してレスポンスを確認
- 02簡潔な構文でリクエストヘッダーとボディパラメータを指定
- 03HTTPie 互換のコマンドをそのまま実行
活用シーン
- 01コーディングエージェントワークフローで API エンドポイントをテスト
- 02HTTP インタラクション用の自動化スクリプトに統合
- 03curl より分かりやすい構文で API をデバッグ
インストール
OS またはパッケージマネージャーを選び、コマンドをターミナルで実行します。
インストール後にxh --versionを実行して確認します。
コマンド例
よく使うコマンドを使う順に並べています。
詳細
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
パッケージマネージャーを使用
| OS | 方法 | コマンド |
|---|---|---|
| Any | Cargo* | cargo install xh --locked |
| Any | Huber | huber install xh |
| Any | Mise | mise use xh --global |
| 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**** | sudo apt install xh |
| Fedora | Copr***** | sudo dnf install xh |
| FreeBSD | FreshPorts | pkg install xh |
| NetBSD | pkgsrc | pkgin install xh |
| Linux & macOS | Nixpkgs | nix-env -iA nixpkgs.xh |
| Linux & macOS | Flox | flox install xh |
| Linux & macOS | Homebrew | brew install xh |
| Linux & macOS | Hermit | hermit install xh |
| macOS | MacPorts | sudo port install xh |
| Windows | Scoop | scoop install xh |
| Windows | Chocolatey | choco install xh |
| Windows | Winget | winget 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
厳密な互換モード
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の出力は短く、チートシート形式です(詳しい出力を見るにはhelpを渡してください)。
デメリット
- HTTPie のすべての機能が実装されているわけではありません。(#4)
- プラグインシステムがありません。
- 全般的に成熟度が低いです。HTTPie は長い歴史があり、十分にテストされています。
- ドキュメントの質が劣ります。
類似または関連するプロジェクト
- curlie - cURL のフロントエンドで、httpie の使いやすさを加えています
- httpie-go - Go で書かれた httpie 風の HTTP クライアント
- curl2httpie - cURL と HTTPie の間でコマンド引数を変換します
