APIドキュメントやPostman、ブラウザのDevToolsのネットワークタブからcurlコマンドをコピーしました。そのリクエストをアプリケーションから再現する必要があります。curlフラグを手動でライブラリコールに変換するのは面倒でエラーが起きやすい——特にヘッダー・認証トークン・フォームデータ・クッキーが絡む場合は。cURL to codeコンバーターがこれを即座に処理します。
cURL to codeの機能
ツールはcurlコマンドを解析し、選択した言語の等価コードを出力します:
- Python —
requestsまたはhttpx - JavaScript —
fetchまたはaxios - Node.js —
node-fetchまたはネイティブhttpsモジュール - Go —
net/http - PHP —
curl拡張またはGuzzleHttp - Ruby —
net/httpまたはFaraday - Java —
HttpClientまたはOkHttp
curlコマンドを貼り付けて言語を選ぶと、動作するコードが得られます。
主要なcurlフラグとその対応
フラグとライブラリの対応関係を理解しておくと、出力のカスタマイズに役立ちます。
-X / --request — HTTPメソッド
curl -X POST https://api.example.com/users
import requests
response = requests.post('https://api.example.com/users')
fetch('https://api.example.com/users', { method: 'POST' })
-H / --header — リクエストヘッダー
curl -H "Content-Type: application/json" \
-H "Authorization: Bearer eyJhbGci..." \
https://api.example.com/users
import requests
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer eyJhbGci...'
}
response = requests.get('https://api.example.com/users', headers=headers)
fetch('https://api.example.com/users', {
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer eyJhbGci...'
}
})
-d / --data — リクエストボディ
curl -X POST https://api.example.com/users \
-H "Content-Type: application/json" \
-d '{"name": "Alice", "email": "[email protected]"}'
import requests, json
data = {"name": "Alice", "email": "[email protected]"}
response = requests.post(
'https://api.example.com/users',
headers={'Content-Type': 'application/json'},
json=data
)
package main
import (
"bytes"
"encoding/json"
"net/http"
)
func main() {
data := map[string]string{"name": "Alice", "email": "[email protected]"}
body, _ := json.Marshal(data)
req, _ := http.NewRequest("POST", "https://api.example.com/users", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
client.Do(req)
}
-F / --form — マルチパートフォームデータ
curl -F "file=@/path/to/report.pdf" \
-F "title=Q1 Report" \
https://api.example.com/upload
import requests
with open('/path/to/report.pdf', 'rb') as f:
files = {'file': f}
data = {'title': 'Q1 Report'}
response = requests.post('https://api.example.com/upload', files=files, data=data)
-u / --user — HTTP Basic認証
curl -u username:password https://api.example.com/protected
import requests
response = requests.get(
'https://api.example.com/protected',
auth=('username', 'password')
)
-k / --insecure — TLS検証をスキップ
curl -k https://localhost:8443/api/health
import requests
response = requests.get('https://localhost:8443/api/health', verify=False)
注意:verify=False / InsecureSkipVerify: true は開発環境と内部ツールのみで使用し、本番環境では絶対に使用しないでください。
実際のワークフロー:ブラウザDevToolsからコードへ
最もよくある使い方は、ブラウザのDevToolsからリクエストを取り出すことです。
- Chrome/Firefox DevToolsを開く → ネットワークタブ
- 複製したいリクエストを見つける
- 右クリック → コピー → cURLとしてコピー
- コンバーターに貼り付ける
- ターゲット言語を選択
このワークフローは特に以下の場面で役立ちます:
- 内部APIのリバースエンジニアリング
- 自動テストでのブラウザ動作の再現
- 公式SDKがないサードパーティサービスとの連携構築
APIドキュメントからのcurlコマンド
ほとんどのAPIドキュメントにはcurlの例が含まれています。ドキュメントとエディタを両方参照しながら手動で変換する代わりに、言語に変換してください。
Stripe APIの例:
curl https://api.stripe.com/v1/charges \
-u sk_test_YOUR_STRIPE_TEST_KEY: \
-d amount=2000 \
-d currency=usd \
-d source=tok_visa \
-d description="Charge for [email protected]"
import stripe
stripe.api_key = 'sk_test_YOUR_STRIPE_TEST_KEY'
charge = stripe.Charge.create(
amount=2000,
currency='usd',
source='tok_visa',
description='Charge for [email protected]'
)
シンプルなケースではコンバーターの出力がそのまま使えます。本番コードには、リトライ・ページネーション・エラーハンドリングを処理する公式SDKが利用可能な場合はそちらを使用してください。
複雑なcurlコマンドの処理
複数の -d フラグ
curlは複数の -d フラグをURLエンコードされた文字列にマージします。コンバーターはこれを正確に処理します:
curl -X POST https://api.example.com/form \
-d "field1=value1" \
-d "field2=value2"
field1=value1&field2=value2 がボディになります。
リダイレクトの追跡(-L)
curl -L https://short.url/abc123
# requestsはデフォルトでリダイレクトを追跡
response = requests.get('https://short.url/abc123', allow_redirects=True)
ツールを試す
任意のcurlコマンドを貼り付け、Python・JavaScript・Go・PHPなどターゲット言語を選ぶと、数秒で動作するコードが得られます。用途:
- APIドキュメントの例を自分の言語に変換
- シェルスクリプトをアプリケーションコードに移行
- 異なる言語を好むチームメンバーとAPIコールの例を共有
- curlの出力とライブラリの動作を比較してHTTPリクエストをデバッグ
変換はすべてブラウザ内で実行されます——サーバーなし、ログなし。