curlコマンドの例
このページは、curlのコマンドの例をまとめる予定のページです。
目次
注意
- このページを作ったひとが備忘のためにまとめているため網羅性はありません。
例
GET リクエスト (レスポンスを出力)
curl "https://example.com/?p1=1&p2=2"
レスポンスを保存 (-o
, --output
)
curl -o test.html "https://example.com/?p1=1&p2=2"
※ -o
(--output
) の次に保存ファイル名を指定します。
POSTリクエスト
curl -X POST "https://example.com"
curl -d "p1=1&p2=2" "https://example.com"
curl -d "p1=1" -d "p2=2" "https://example.com"
※ -d
(--data
) の次にパラメータを「名前=値」形式で指定します。
※ -d
(--data
) を使用していれば -X POST
がなくてもPOSTになります。
HEADリクエスト (-I
, --head
)
curl -I "https://example.com/?p1=1&p2=2"
GETやPOSTのレスポンスにヘッダを含める (-i
, --include
)
curl -i "https://example.com/?p1=1&p2=2"
ユーザーエージェントを指定 (-A
, --user-agent
)
curl -A "Test Agent" "https://example.com/?p1=1&p2=2"
※ -A
(--user-agent
) の次にユーザーエージェント名を指定します。
リクエストヘッダを指定 (-H
, --header
)
curl -H "X-Test-Header: test" -H "X-Test-Header2: test2" "https://example.com/?p1=1&p2=2"
※ -H
(--header
) の次にヘッダを指定します。
Cookieの保存と指定 (-c
, --cookie-jar
, -b
, --cookie
)
curl -c "cookie.txt" -d "user=user1&pass=pass1" "https://example.com/login/"
curl -b "cookie.txt" "https://example.com/mypage/"
-c
(--cookie-jar
) でCookieを保存して、-b
(--cookie
) で使用します。
ログインページで認証したあと、認証が必要なページにアクセスするときなどに使用します。
Basic認証付きのページにアクセス (--basic
)
curl --basic -u user:pass "http://example.com/path/to/file.html"
※ --basic
と --u
(-user
) で Basic認証付きのページにアクセスします。(ユーザー名とパスワードは ユーザー名:パスワード
の形式で指定します)
プログレスバーを表示しない (-s
, --silent
)
curl -sS "https://example.com/?p1=1&p2=2"
※ -s
(--silent
) がプログレスバーやエラーを表示しないオプション (サイレントモード)、-S
(--show-error
) がサイレントモード中でもエラーを表示するオプションです。
SSL証明書を無視する (-k
, --insecure
)
curl -k "https://example.com/?p1=1&p2=2"
※ -k
(--insecure
) によって、問題のあるSSL証明書 (自己証明書など) のエラーを無視します。
詳細表示 (-v
, --verbose
)
curl -v "https://example.com/?p1=1&p2=2"
-v
(--verbose
) でリクエストとレスポンスの詳細を出力します。
* Trying 93.184.216.34:443...
* Connected to example.com (93.184.216.34) port 443 (#0)
* schannel: disabled automatic use of client certificate
* schannel: ALPN, offering http/1.1
* schannel: ALPN, server accepted to use http/1.1
> GET /?p1=1&p2=2 HTTP/1.1
> Host: example.com
> User-Agent: curl/7.79.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Age: 461834
< Cache-Control: max-age=604800
< Content-Type: text/html; charset=UTF-8
< Date: Fri, 25 Mar 2022 10:40:22 GMT
< Etag: "3147526947"
< Expires: Fri, 01 Apr 2022 10:40:22 GMT
< Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
< Server: ECS (oxr/836D)
< Vary: Accept-Encoding
< X-Cache: HIT
< Content-Length: 1256
<
(以下レスポンスボディ)