インフラエンジニア向けcurlコマンドの使い方

curlコマンドはコマンドラインでWEBサーバーなどにHTTP/HTTPSアクセスするためのツールです。最近ではWindowsでも利用でき、ネットワークやサーバー、アプリ開発に携わる方であれば必須のツールだと思いますのでcurlの使い方を詳しく解説します。

証明書エラーを無視する

期限切れや無効なSSL証明書でHTTPS接続するとエラーで止まってしまいます。SSLエラーでも継続してアクセスしたい場合は「-k」オプションでSSLエラーを無視します。

例:curl -k https://expample.com

応答ヘッダーを出力する

応答ヘッダーを含めて表示する

サーバーからの応答ヘッダーを含めて表示したい場合は「-i」オプションを使います。

例:curl -i https://google.com

実行結果
C:\Users\foo>curl -i https://google.com
HTTP/1.1 301 Moved Permanently
Location: https://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Thu, 02 Feb 2023 00:27:15 GMT
Expires: Sat, 04 Mar 2023 00:27:15 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 220
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.com/">here</A>.
</BODY></HTML>

C:\Users\foo>

応答ヘッダーのみ表示する

ボディ部分は不要なので応答ヘッダーだけ見たいという場合は「-I」オプションを使います。

例:curl -I https://google.com

実行結果
C:\Users\foo>curl -I https://google.com
HTTP/1.1 301 Moved Permanently
Location: https://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Thu, 02 Feb 2023 00:30:35 GMT
Expires: Sat, 04 Mar 2023 00:30:35 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 220
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000


C:\Users\foo>

リダイレクトを追跡する

リダイレクトしている場合、初期動作ではリダイレクト先にアクセスしません。リダイレクト先に続けてアクセスするには「-L」オプションを使います。

例:curl -L https://google.com

応答ヘッダーを出力させてリダイレクトしている様子を見てみます。まずはリダイレクトを追跡しない場合。

実行結果
C:\Users\foo>curl -I https://google.com
HTTP/1.1 301 Moved Permanently
Location: https://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Thu, 02 Feb 2023 00:34:40 GMT
Expires: Sat, 04 Mar 2023 00:34:40 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 220
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000


C:\Users\foo>

次に302リダイレクトを追跡する場合。

実行結果
C:\Users\foo>curl -I -L https://google.com
HTTP/1.1 301 Moved Permanently
Location: https://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Thu, 02 Feb 2023 00:35:38 GMT
Expires: Sat, 04 Mar 2023 00:35:38 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 220
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000

HTTP/1.1 200 OK
Content-Type: text/html; charset=ISO-8859-1
P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
Date: Thu, 02 Feb 2023 00:35:38 GMT
Server: gws
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Transfer-Encoding: chunked
Expires: Thu, 02 Feb 2023 00:35:38 GMT
Cache-Control: private
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000


C:\Users\foo>

ファイルに書き出す

応答内容をファイルに書き出す場合は「-o」オプションを使います。

例:curl -o foo.txt https://google.com

実行結果
C:\Users\foo>curl -o foo.txt https://google.com
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   220  100   220    0     0   1332      0 --:--:-- --:--:-- --:--:--  1341

C:\Users\foo>type foo.txt
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.com/">here</A>.
</BODY></HTML>

C:\Users\foo

プロキシを使う

プロキシが必要な場合、環境変数「HTTP_PROXY」と「HTTPS_PROXY」でプロキシ設定をおこなう必要があります。ただし環境変数を設定したくない・設定できない、一時的に使えれば良いという場合は「-x」オプションでプロキシを設定できます。

プロキシサーバーが「example.com:8080」で認証が不要な場合。

例:curl -x http://example.com:8080 https://google.com

認証が必要な場合は「@」の前に「ユーザー名:パスワード」を付与します。ユーザー名が「foo」でパスワードが「bar」の場合は次のように実行します。

例:curl -x http://foo:bar@example.com:8080 https://google.com

出力レベルを変える

冗長出力(送信ヘッダーも表示したい)

送信ヘッダーと受信ヘッダーに加えて冗長な情報も出力したい場合は「-v」オプションを使います。

例:curl -v https://goog.e.com

実行結果
C:\Users\foo>curl -v https://google.com
*   Trying 2404:6800:4004:81c::200e:443...
* Connected to google.com (2404:6800:4004:81c::200e) port 443 (#0)
* schannel: disabled automatic use of client certificate
* ALPN: offers http/1.1
* ALPN: server accepted http/1.1
> GET / HTTP/1.1
> Host: google.com
> User-Agent: curl/7.83.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< Location: https://www.google.com/
< Content-Type: text/html; charset=UTF-8
< Date: Thu, 02 Feb 2023 00:51:54 GMT
< Expires: Sat, 04 Mar 2023 00:51:54 GMT
< Cache-Control: public, max-age=2592000
< Server: gws
< Content-Length: 220
< X-XSS-Protection: 0
< X-Frame-Options: SAMEORIGIN
< Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
<
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.com/">here</A>.
</BODY></HTML>
* Connection #0 to host google.com left intact

C:\Users\foo>

進捗状況を表示しない

スクリプトで実行させる場合など、進捗状況の表示が不要な場合は「-s」を使います。

例:curl -s -o foo.txt https://google.com

実行結果
C:\Users\foo>curl -s -o foo.txt https://google.com

C:\Users\foo>

ユーザーエージェントを設定する

curlのユーザーエージェントはデフォルトでは「curl/バージョン」です(バージョンはcurlのバージョンに読みかえてください)。

curlが送信するユーザーエージェントを変更する場合は「-A」オプションを使います。

例:curl -A ユーザーエージェント https://google.com

ユーザーエージェントとして「MY-USER-AGENT」を送信してみます。送信ヘッダーを見るために「-v」オプションを付けています。

実行結果
C:\Users\foo>curl -A MY-USER-AGENT -v https://google.com
*   Trying 2404:6800:4004:808::200e:443...
* Connected to google.com (2404:6800:4004:808::200e) port 443 (#0)
* schannel: disabled automatic use of client certificate
* ALPN: offers http/1.1
* ALPN: server accepted http/1.1
> GET / HTTP/1.1
> Host: google.com
> User-Agent: MY-USER-AGENT
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< Location: https://www.google.com/
< Content-Type: text/html; charset=UTF-8
< Date: Thu, 02 Feb 2023 01:02:16 GMT
< Expires: Sat, 04 Mar 2023 01:02:16 GMT
< Cache-Control: public, max-age=2592000
< Server: gws
< Content-Length: 220
< X-XSS-Protection: 0
< X-Frame-Options: SAMEORIGIN
< Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
<
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.com/">here</A>.
</BODY></HTML>
* Connection #0 to host google.com left intact

C:\Users\foo>

HTTPヘッダーを追加する

追加したいHTTPヘッダーがある場合は「-H」を使います。

例:curl -H “X-Forwarded-For: 10.0.0.1” https://google.com

複数のヘッダーを追加したい場合は「-H」オプションを複数使えます。

例:curl -H “X-Forwarded-For: 10.0.0.1” -H “FOO: BAR” https://google.com

HTTPヘッダーが追加されているのか確認してみます。送信するHTTPヘッダーを見るために「-v」オプションを付けています。

実行結果
C:\Users\foo>curl -v -H "X-Forwarded-For: 10.0.0.1" -H "FOO: BAR" https://google.com
*   Trying 2404:6800:4004:823::200e:443...
* Connected to google.com (2404:6800:4004:823::200e) port 443 (#0)
* schannel: disabled automatic use of client certificate
* ALPN: offers http/1.1
* ALPN: server accepted http/1.1
> GET / HTTP/1.1
> Host: google.com
> User-Agent: curl/7.83.1
> Accept: */*
> X-Forwarded-For: 10.0.0.1
> FOO: BAR
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< Location: https://www.google.com/
< Content-Type: text/html; charset=UTF-8
< Date: Thu, 02 Feb 2023 01:07:14 GMT
< Expires: Sat, 04 Mar 2023 01:07:14 GMT
< Cache-Control: public, max-age=2592000
< Server: gws
< Content-Length: 220
< X-XSS-Protection: 0
< X-Frame-Options: SAMEORIGIN
< Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
<
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.com/">here</A>.
</BODY></HTML>
* Connection #0 to host google.com left intact

C:\Users\foo>

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)