curl
コマンドは、指定した URL に対して HTTP, HTTPS, FTP, SCP, SFTP などのプロトコルを用いたデータ転送 を行うコマンドです。
Web API の動作確認、ファイルのダウンロード、サーバ応答の調査など、ネットワーク関連の幅広い用途に利用されます。
構文(Syntax)
curl [オプション] [URL...]
主なオプション一覧
オプション | 説明 | 使用例 |
---|---|---|
-o FILE | 出力先ファイルを指定 | curl -o index.html https://example.com |
-O | URL のファイル名で保存 | curl -O https://example.com/file.zip |
-L | リダイレクトを追跡 | curl -L http://example.com |
-I | HTTP ヘッダのみ取得 | curl -I https://example.com |
-X METHOD | HTTP メソッドを指定 (GET/POST/PUT/DELETE など) | curl -X POST https://example.com/api |
-d DATA | POST データを送信 | curl -d "user=alice&pass=1234" https://example.com/login |
-H HEADER | 追加の HTTP ヘッダを送信 | curl -H "Authorization: Bearer TOKEN" https://example.com/api |
-u USER:PASS | Basic認証でアクセス | curl -u alice:secret https://example.com |
-k | SSL 証明書の検証を無効化 | curl -k https://selfsigned.local |
-s | 進行状況やエラーを非表示 (silent) | curl -s https://example.com |
-v | 詳細な通信ログを表示 (verbose) | curl -v https://example.com |
--limit-rate RATE | 転送速度を制限 | curl --limit-rate 100k https://example.com/file.zip |
-C - | 中断したダウンロードを再開 | curl -C - -O https://example.com/file.zip |
実行例
Webページを取得して標準出力に表示
curl https://example.com
Webページをファイルに保存
curl -o index.html https://example.com
サーバのレスポンスヘッダを確認
curl -I https://example.com
出力例:
HTTP/1.1 200 OK
Date: Wed, 21 Aug 2024 12:34:56 GMT
Content-Type: text/html; charset=UTF-8
POST データを送信
curl -X POST -d "name=alice&age=20" https://example.com/form
JSON API にリクエスト
curl -H "Content-Type: application/json" \
-d '{"user":"alice","pass":"1234"}' \
https://example.com/api/login
Basic認証を使ってアクセス
curl -u alice:password https://example.com/secure
リダイレクトを追跡
curl -L http://example.com
ダウンロードを途中から再開
curl -C - -O https://example.com/largefile.iso
エラー例(存在しないページ)
curl https://example.com/notfound
出力例:
curl: (22) The requested URL returned error: 404 Not Found
関連コマンド
wget
: ファイルダウンロード専用のコマンドhttpie
: 人間が読みやすい HTTP クライアントscp
/sftp
: SSH 経由でファイルを転送ping
/traceroute
: ネットワーク疎通確認
備考
curl
は非常に多機能で、FTP/SFTP でのファイル操作、SMTP を使ったメール送信、WebSocket の簡易確認などにも利用可能です。- SSL 証明書の検証はデフォルトで有効になっています。自己署名証明書を扱う場合は
-k
を付ける必要があります。 - API テストやスクリプト内でのデータ送受信において、
curl
は最も広く利用されるツールの一つです。
参考
- manページ: man7.org curl(1)
- 公式サイト: https://curl.se/
コメント