curlコマンドってなに?
はじめに
これまで全くcurlコマンドを触って来なかったので調べてみました。
初心者向けにcurlコマンドの使い方をざっくりと記載しているのでご覧ください!
curlコマンドとは?
以下に主な特徴を記載しました。
- インターネット上のデータを取得/送信するためのコマンドラインツール
- 正式名称は「Client URL」の略で、URLを使ってサーバーと通信する
- HTTPやHTTPSなどのプロトコルを使って、WebページやAPIとやり取りを行う
curlコマンドってどんな場面で使われるの?
では実際にどんな場面でcurlコマンドが使われるのでしょうか?
ネットワークやサーバーの状態確認、APIの疎通テスト、監視ツールとの連携などかなり多岐にわたります。
具体的な例で言うと、
「お客さんに提供しているとあるサイトにアクセスできるか確認したい!」
「でもブラウザなどのGUIが使えない」
そんな時にCLIベースで使えるのがcurlコマンドです。
次の項目で実際の使い方を記載します。
curlコマンドを実行してみた
curlコマンドの使い方
以下のようにcurlの後に、調査対象のURLを記載します。
これが基本系でcurlとURLの間に様々なオプションを入れることで使いこなすことが可能です。オプションについては後続の章で記載します。
curl <オプション> <URL>
// 例
curl -I https://www.google.com
curlコマンドの種類
curlコマンドには様々なオプションがあるので代表的なものに絞って紹介していきます。
基本構文
curl <URL>
実行例
実行結果は以下の画像の通りです。オプションを付けないと判断が付きにくく分かりにくいですね。
-Iオプション
「-I」オプションではHTTPレスポンスヘッダーの情報を取得します。
ざっくり言うと「Webサイトの表紙情報(ヘッダー)だけを見せて!」という命令で、実際のページ内容は見ずにサーバーがどんな状態かだけ確認します。
実行例
実行結果がHTTP/2 200なので通信は正常です。
$ curl -I https://www.google.com
HTTP/2 200
content-type: text/html; charset=ISO-8859-1
content-security-policy-report-only: object-src 'none';base-uri 'self';script-src 'nonce-EOLPh5UMYxFkez2n4Wvt9Q' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inline' https: http:;report-uri https://csp.withgoogle.com/csp/gws/other-hp
accept-ch: Sec-CH-Prefers-Color-Scheme
p3p: CP="This is not a P3P policy! See g.co/p3phelp for more info."
date: Thu, 31 Jul 2025 13:31:40 GMT
server: gws
x-xss-protection: 0
x-frame-options: SAMEORIGIN
expires: Thu, 31 Jul 2025 13:31:40 GMT
cache-control: private
set-cookie: AEC=AVh_V2iAd6Zahy7SPjI_lGR7wfMIGxlLPdqKg799zcKdeMjICuv9ntFnv-U; expires=Tue, 27-Jan-2026 13:31:40 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax
set-cookie: NID=525=ETlzqsOb-ioqyO6cghVmUJ28frhiEj_XFstyXBp3SlcRS11SjuyxkeqXSeWI1jUUyDvBUk6osB6QBw9L3nShiAXlQeFIM1VpmcDrqNZh08CsLVTu6aAwEce_HMdTUTnZQi-TrYDfsT2g_Nu7RpW4fkq4PMvfg-3BeAdWAzHQXmvfVqnbljgF7wbp5Wk8kGFmhESX6Gjgbx0pGQ; expires=Fri, 30-Jan-2026 13:31:40 GMT; path=/; domain=.google.com; HttpOnly
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
-vオプション
「-v」オプションでは実行結果の詳細を取得します。
通信の細かい流れ(どんなプロトコル使ってるか、どんなヘッダー送ってるか、SSLの確認結果などなど)が全部見えるようになります。
実行例
通信の細かい流れが見えるようになるので返ってくる情報も多くなります。抜粋して記載します。
~$ curl -v https://www.google.com
// DNS名前解決/接続開始
* Host www.google.com:443 was resolved.
* IPv6: 2404:6800:4004:826::2004
* IPv4: 172.217.161.36
* Trying 172.217.161.36:443...
* Connected to www.google.com (172.217.161.36) port 443
* ALPN: curl offers h2,http/1.1
// TLSハンドシェイクと証明書
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* CAfile: /etc/ssl/certs/ca-certificates.crt
* CApath: /etc/ssl/certs
* TLSv1.3 (IN), TLS handshake, Server hello (2):
// ... 略
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384 / X25519 / id-ecPublicKey
* ALPN: server accepted h2
* Server certificate:
* subject: CN=www.google.com
* start date: Jul 7 08:35:54 2025 GMT
* expire date: Sep 29 08:35:53 2025 GMT
* subjectAltName: host "www.google.com" matched cert's "www.google.com"
* issuer: C=US; O=Google Trust Services; CN=WR2
* SSL certificate verify ok.
// ... 略
// レスポンス内容とヘッダー
// こちらもステータスコード200で返ってきているので通信は正常です
< HTTP/2 200
< date: Thu, 31 Jul 2025 14:03:03 GMT
< expires: -1
< cache-control: private, max-age=0
< content-type: text/html; charset=ISO-8859-1
< content-security-policy-report-only: object-src 'none';base-uri 'self';script-src 'nonce-vwM9QQotuWsclL5Zv_BnkQ' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inline' https: http:;report-uri https://csp.withgoogle.com/csp/gws/other-hp
< accept-ch: Sec-CH-Prefers-Color-Scheme
< p3p: CP="This is not a P3P policy! See g.co/p3phelp for more info."
< server: gws
< x-xss-protection: 0
< x-frame-options: SAMEORIGIN
< set-cookie: AEC=AVh_V2gz-v-6Z82ke8TTuL22-tIgs3YQK1DM03WZOxvwGVSO3WVxAMdXovM; expires=Tue, 27-Jan-2026 14:03:03 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax
< set-cookie: NID=525=Vnidvkjga4hItB0PyrwlfOWo299fkfqJH1FXdKIx7Llpzz4KX5b060cB6jpPOHfYTtd1pol-qwoiaIyKGdkMq4EuO2naH1DvbwJif-RdII6n1iQxZLnnHMRi4ESRL4utGo9eggpLl74TnP8W2p8P5wiGGOytX3LvMvfSEx4XRXFJznM0WQs-YEAERjH_XUCWcDxOPXfdej-M9_I9CA; expires=Fri, 30-Jan-2026 14:03:03 GMT; path=/; domain=.google.com; HttpOnly
< alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
< accept-ranges: none
< vary: Accept-Encoding
-kオプション
「-k」オプションはSSL証明書の検証をスキップするので注意が必要です。
なので実際には、開発/テスト実施時や自己証明書を発行したサーバをテストする際に使うことが多いです。
実行例
https://self-signed.badssl.com という自己証明書を使っているサイトで検証しました。
-k を使わないとエラーになります。
$ curl -kI https://self-signed.badssl.com
HTTP/1.1 200 OK
Server: nginx/1.10.3 (Ubuntu)
Date: Thu, 31 Jul 2025 14:26:28 GMT
Content-Type: text/html
Content-Length: 502
Last-Modified: Tue, 29 Jul 2025 21:00:51 GMT
Connection: keep-alive
ETag: "68893683-1f6"
Cache-Control: no-store
Accept-Ranges: bytes
// 自己証明書を使っているサイトなので「-k」をつけないとエラーになる
$ curl -I https://self-signed.badssl.com
curl: (60) SSL certificate problem: self-signed certificate
More details here: https://curl.se/docs/sslcerts.html
curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.
-Lオプション
「-L」オプションはリダイレクトを追跡するために使われます。
実行例
GoogleのDNSサーバに対してcurlコマンドを実行して、リダイレクト先のGoogleのサイトに到達しています。
$ curl -LI https://8.8.8.8
// GoogleのDNSサーバに移動
HTTP/2 302
// ... 略
location: https://dns.google/
date: Fri, 01 Aug 2025 12:28:12 GMT
content-type: text/html; charset=UTF-8
server: HTTP server (unknown)
content-length: 216
x-xss-protection: 0
x-frame-options: SAMEORIGIN
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
// HTTP/2 200なので接続成功
HTTP/2 200
content-security-policy: object-src 'none';base-uri 'self';script-src 'nonce-ungtR9bsXbhUxQ2i20g6WQ' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inline' https: http:;report-uri https://csp.withgoogle.com/csp/honest_dns/1_0;frame-ancestors 'none'
strict-transport-security: max-age=31536000; includeSubDomains; preload
x-content-type-options: nosniff
content-security-policy-report-only: script-src 'none'; form-action 'none'; frame-src 'none'; report-uri https://csp.withgoogle.com/csp/scaffolding/ntdsgswbsc:55:0
cross-origin-opener-policy-report-only: same-origin; report-to=ntdsgswbsc:55:0
report-to: {"group":"ntdsgswbsc:55:0","max_age":2592000,"endpoints":[{"url":"https://csp.withgoogle.com/csp/report-to/scaffolding/ntdsgswbsc:55:0"}],}
server: scaffolding on HTTPServer2
x-xss-protection: 0
x-frame-options: SAMEORIGIN
date: Fri, 01 Aug 2025 12:24:54 GMT
expires: Fri, 01 Aug 2025 12:29:54 GMT
cache-control: public, max-age=300
content-type: text/html; charset=UTF-8
age: 198
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
accept-ranges: none
vary: Accept-Encoding
-uオプション
「-u」オプションはユーザー名とパスワードを使って認証を行います。先ほど説明した「-L」オプションを使って、リダイレクト先で認証を行う必要がある場合に便利です。
実行例
今回は実際の実行結果はないですすみません..
$ curl -u myuser:mypassword https://example.com/api/data
-oオプション/-Oオプション
「-o」オプションはURLから指定の場所に名前を付けてファイルを保存します。
保存場所やファイル名を特に指定する必要がない場合は「-O」オプションを使ってカレントディレクトリにそのままファイルを保存します。
実行例
「-o」オプションの実行例です。
$ curl -o tmp/test.yaml https://github.com/CyberAgent/intelligent-hpa/blob/master/manifests/intelligent-hpa.yaml
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1091k 0 1091k 0 0 1176k 0 --:--:-- --:--:-- --:--:-- 1176k
$ ls -l tmp/
total 1092
-rw-rw-r-- 1 ubuntu ubuntu 1117888 Aug 1 21:54 test.yaml
// ディレクトリを指定するとエラーになります。
$ curl -o tmp https://github.com/CyberAgent/intelligent-hpa/blob/master/manifests/intelligent-hpa.yaml
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0Warning: Failed to open the file tmp: Is a directory
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
curl: (23) Failure writing output to destination
「-O」オプションの実行例です。
$ curl -O https://github.com/CyberAgent/intelligent-hpa/blob/master/manifests/intelligent-hpa.yaml
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1417k 0 1417k 0 0 1255k 0 --:--:-- 0:00:01 --:--:-- 1256k
$ ls -l
total 2512
-rw-rw-r-- 1 ubuntu ubuntu 1451079 Aug 1 21:56 intelligent-hpa.yaml
おわりに
curlコマンドについて簡単にですが記載しました。
私も基本的な部分まだ扱えておらず、オプション等を活かしきれていないので、一つずつ覚えていきたいです。
以上curlコマンドのお話でした。