基本的な使い方
シンプルなリクエスト
curl https://example.com # GET リクエスト curl -o file.html https://url # ファイルに保存 curl -O https://url/file.tar.gz # リモート名で保存 curl -L https://url # リダイレクトに従う
よく使うフラグ
-sサイレントモード(進捗なし)
-Sサイレントモードでもエラーを表示
-fHTTP エラー時にサイレントで失敗
-Lリダイレクトに従う
-o file出力をファイルに書き込む
-Oリモートのファイル名で保存
-C -中断されたダウンロードを再開
--max-time 3030 秒後にタイムアウト
HTTP メソッド
GET と HEAD
curl https://api.example.com/users curl -I https://example.com # HEAD(ヘッダーのみ) curl -i https://example.com # レスポンスヘッダーを含む
POST
curl -X POST https://api.example.com/users \ -H "Content-Type: application/json" \ -d '{"name":"Jo","email":"[email protected]"}'
PUT と PATCH と DELETE
curl -X PUT https://api.example.com/users/1 \ -d '{"name":"Updated"}' curl -X PATCH https://api.example.com/users/1 \ -d '{"email":"[email protected]"}' curl -X DELETE https://api.example.com/users/1
ヘッダー
ヘッダーの設定
curl -H "Content-Type: application/json" URL curl -H "Accept: text/html" URL curl -H "X-Custom: value" URL curl -H "Header1: v1" -H "Header2: v2" URL
レスポンスヘッダー
-i出力にレスポンスヘッダーを含める
-Iヘッダーのみ取得(HEAD)
-D fileレスポンスヘッダーをファイルに保存
-w '%{http_code}'HTTP ステータスコードを表示
認証
Basic 認証とトークン認証
curl -u user:pass https://api.example.com curl -H "Authorization: Bearer TOKEN" URL curl -u user:pass --digest URL curl --negotiate -u : URL # Kerberos/SPNEGO
認証方式
-u user:passBasic 認証
--digestHTTP Digest 認証
--negotiateKerberos/SPNEGO 認証
--ntlmNTLM 認証
-n~/.netrc の認証情報を使用
データとフォーム
データの送信
curl -d "key=val&key2=val2" URL # フォーム URL エンコード curl -d @data.json URL # ファイルからデータを送信 curl --data-raw '{"raw":"json"}' URL curl --data-urlencode "q=hello world" URL
ファイルのアップロード
curl -F "[email protected]" URL curl -F "[email protected];type=application/pdf" URL curl -F "field=value" -F "[email protected]" URL
マルチパート vs URL エンコード
-dapplication/x-www-form-urlencoded
-Fmultipart/form-data
--json短縮形: Content-Type と Accept を JSON に設定
-T filePUT でファイルをアップロード
SSL/TLS
証明書オプション
curl --cacert ca.pem URL # カスタム CA バンドル curl --cert client.pem URL # クライアント証明書 curl --cert client.pem --key key.pem URL curl -k URL # TLS 検証をスキップ(開発時のみ)
TLS フラグ
-k / --insecureTLS 証明書の検証をスキップ
--cacert fileカスタム CA 証明書を使用
--cert fileクライアント証明書
--key fileクライアント秘密鍵
--tlsv1.2最小 TLS 1.2 を強制
--tlsv1.3最小 TLS 1.3 を強制
出力とデバッグ
詳細出力とトレース
curl -v URL # 詳細出力 curl --trace dump.txt URL # ファイルにフルトレース curl --trace-ascii - URL # 標準出力にトレース curl -w "\n%{http_code}\n" URL # カスタム出力フォーマット
Write-Out 変数
%{http_code}HTTP レスポンスステータスコード
%{time_total}合計時間(秒)
%{time_connect}接続確立までの時間
%{size_download}ダウンロードしたバイト数
%{speed_download}平均ダウンロード速度
%{redirect_url}リダイレクト URL(あれば)
%{ssl_verify_result}SSL 検証結果(0 = OK)
Write-Out の例
curl -s -o /dev/null -w \ "code: %{http_code}\ntime: %{time_total}s\n" \ https://example.com
よくあるパターン
API ワークフロー
# JSON を取得して jq にパイプ curl -s https://api.example.com/data | jq '.items[]' # 認証付きで JSON を POST curl -s -H "Authorization: Bearer $TOKEN" \ --json '{"key":"val"}' https://api.example.com
ダウンロードパターン
# プログレスバー付きでダウンロード curl -# -O https://releases.example.com/v2.tar.gz # 中断されたダウンロードを再開 curl -C - -O https://releases.example.com/v2.tar.gz # 複数ファイルをダウンロード curl -O https://url/file1 -O https://url/file2
スクリプトのヘルパー
# URL に到達できるか確認 curl -sf -o /dev/null https://example.com && echo OK # Cookie を保存して再利用 curl -c cookies.txt -b cookies.txt URL # リクエストをレート制限 curl --limit-rate 100k URL
プロキシとネットワーク
プロキシ設定
curl -x http://proxy:8080 URL curl -x socks5://proxy:1080 URL curl --proxy-user user:pass -x http://proxy:8080 URL curl --noproxy "*.local,localhost" URL
DNS と解決
--resolve host:port:addrDNS 解決を addr に強制
--dns-servers 8.8.8.8カスタム DNS サーバーを使用
--interface eth0特定のネットワークインターフェースを使用
-4 / -6IPv4 / IPv6 を強制
設定と応用
設定ファイル
# ~/.curlrc — デフォルトオプション --silent --location --max-time 30 # 設定ファイルを明示的に使用 curl -K myconfig.txt URL
便利なフラグ
--retry 3一時的なエラー時に再試行
--retry-delay 2再試行間の遅延(秒)
--compressedgzip/br を要求して展開
--limit-rate 100k転送速度を制限
-Z並列転送(curl 7.66+)
--create-dirs-o のパスディレクトリを作成