curl入門:GET・POST・PUT・認証・クッキーの使い方まとめ

スポンサーリンク

curl はコマンドラインからHTTPリクエストを送るツールです。APIの動作確認・デバッグ・スクリプトによる自動化など、開発現場で広く使われています。

インストール確認

curl --version

macOS・多くのLinuxには標準でインストール済みです。

GET リクエスト

基本

curl https://api.example.com/users

クエリストリング

URLに直接付けます。

curl "https://api.example.com/users?page=1&limit=20"
curl "https://api.example.com/search?q=golang&sort=created&order=desc"

レスポンスをファイルに保存

curl -o response.json https://api.example.com/users

リダイレクトに追従する

curl -L https://example.com

POST リクエスト

JSON ボディを送る

curl -X POST https://api.example.com/users   -H "Content-Type: application/json"   -d '{"name": "田中", "email": "tanaka@example.com"}'

JSON をファイルから読み込む

curl -X POST https://api.example.com/users   -H "Content-Type: application/json"   -d @request.json

ネストしたオブジェクト

curl -X POST https://api.example.com/orders   -H "Content-Type: application/json"   -d '{
    "user": {"id": 1},
    "items": [
      {"product_id": 10, "quantity": 2},
      {"product_id": 20, "quantity": 1}
    ],
    "note": "午前中に配達希望"
  }'

フォームデータ(application/x-www-form-urlencoded)

curl -X POST https://api.example.com/login   -d "username=admin&password=secret"

複数フィールドは -d を複数使っても同じです。

curl -X POST https://api.example.com/login   -d "username=admin"   -d "password=secret"

マルチパート(ファイルアップロード)

curl -X POST https://api.example.com/upload   -F "file=@image.png"   -F "description=プロフィール画像"

PUT リクエスト

curl -X PUT https://api.example.com/users/1   -H "Content-Type: application/json"   -d '{"name": "田中 太郎", "email": "tanaka@example.com"}'

PATCH リクエスト

一部だけ更新する場合は PATCH を使います。

curl -X PATCH https://api.example.com/users/1   -H "Content-Type: application/json"   -d '{"name": "田中 太郎"}'

DELETE リクエスト

curl -X DELETE https://api.example.com/users/1

ヘッダーの指定

-H で任意のヘッダーを追加します。複数指定する場合は -H を繰り返します。

curl https://api.example.com/data   -H "Accept: application/json"   -H "X-Custom-Header: myvalue"

認証

Bearer Token(JWT など)

curl https://api.example.com/me   -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIs..."

API Key をヘッダーで渡す

curl https://api.example.com/data   -H "X-API-Key: your-api-key-here"

API Key をクエリストリングで渡す

curl "https://api.example.com/data?api_key=your-api-key-here"

Basic 認証

curl https://api.example.com/admin   -u username:password

セッション・クッキー

クッキーをファイルに保存する(ログイン)

curl -X POST https://example.com/login   -d "username=admin&password=secret"   -c cookies.txt

-c cookies.txt でレスポンスの Set-Cookie をファイルに保存します。

保存したクッキーを送る

curl https://example.com/dashboard   -b cookies.txt

ログイン → 認証が必要なAPIを叩く(一連の流れ)

# 1. ログインしてセッションを保存
curl -X POST https://example.com/login   -d "username=admin&password=secret"   -c session.txt

# 2. セッションを使ってAPIにアクセス
curl https://example.com/api/profile   -b session.txt

トークン認証(ログインしてトークンを取得→使用)

# 1. ログインしてトークンを取得
TOKEN=$(curl -s -X POST https://api.example.com/auth/login   -H "Content-Type: application/json"   -d '{"email":"user@example.com","password":"secret"}'   | jq -r '.token')

# 2. トークンを使ってAPIにアクセス
curl https://api.example.com/me   -H "Authorization: Bearer $TOKEN"

-s はプログレスバーを非表示にするオプションです。jq -r '.token' でJSONからトークンを取り出しています。

レスポンス・デバッグ

ステータスコードだけ確認

curl -s -o /dev/null -w "%{http_code}" https://api.example.com/users

リクエスト・レスポンスヘッダーを表示

curl -v https://api.example.com/users

レスポンスヘッダーのみ表示

curl -I https://api.example.com/users

よく使うオプション早見表

オプション 説明
-X METHOD HTTPメソッドを指定(GET・POST・PUT・DELETE)
-H "Key: Value" リクエストヘッダーを追加
-d 'data' リクエストボディを指定
-d @file ファイルの内容をボディとして送る
-F "key=@file" マルチパート(ファイルアップロード)
-u user:pass Basic認証
-c file クッキーをファイルに保存
-b file ファイルのクッキーを送信
-o file レスポンスをファイルに保存
-s プログレスバーを非表示
-L リダイレクトに追従
-v 詳細ログ(デバッグ用)
-I レスポンスヘッダーのみ取得
-w "%{http_code}" 指定フォーマットで情報を出力

まとめ

# GET + クエリストリング
curl "https://api.example.com/users?page=1&limit=20"

# POST + JSON
curl -X POST https://api.example.com/users   -H "Content-Type: application/json"   -d '{"name": "田中"}'

# Bearer Token 認証
curl https://api.example.com/me   -H "Authorization: Bearer $TOKEN"

# セッションクッキーの保存と送信
curl -X POST https://example.com/login -d "..." -c session.txt
curl https://example.com/api -b session.txt

curlのレスポンスJSONを整形・絞り込みするには「macでjqを使う:JSONをコマンドラインで操作する方法」を参照してください。