10 リアルタイムの例での cURL コマンドの使用法
公開: 2015-02-12cURL は、URL 構文を使用してデータを取得または送信するためのコマンド ライン ツールです。
開発者またはサポート部門で働いている場合は、Web アプリケーションのトラブルシューティングを行うためのcURL コマンドの使用法に注意する必要があります。 cURL は、Windows、MAC、および UNIX で使用できるクロスプラットフォーム ユーティリティです。
以下は、最もよく使用される構文の一部と、役立つ例です。
URLに接続できるか確認する
UNIX システムで作業していて、外部 URL に接続しようとしている場合、最初に行うことは、curl を介して URL にアクセスできるかどうかを確認することです。
curl yoururl.com
出力はスローされません。 ただし、サーバーが接続できない場合は、ホストを解決できないなどのエラーが発生します。
[root@gf-lab tmp]# curl helloitdoesntexist.com curl: (6) Could not resolve host: helloitdoesntexist.com; Unknown error [root@gf-lab tmp]#
URL/URI 出力をファイルに保存
URL または URI の内容を特定のファイルに保存する必要がある場合は、次の構文を使用できます。
curl https://yoururl.com > yoururl.html
元:
[root@gf-lab tmp]# curl https://gf.dev > /tmp/gfhtml % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 18557 0 18557 0 0 72565 0 --:--:-- --:--:-- --:--:-- 72772 [root@gf-lab tmp]#
上記の例では、すべてのコンテンツを gf.dev から /tmp/gf.html に保存します
リクエストとレスポンスのヘッダーを表示
問題があり、検証したい場合は、期待される要求と応答ヘッダーを取得しています。
curl -v yoururl.com
元:
[root@gf-lab tmp]# curl -v https://geekflare.com * About to connect() to geekflare.com port 443 (#0) * Trying 104.25.134.107... * Connected to geekflare.com (104.25.134.107) port 443 (#0) * Initializing NSS with certpath: sql:/etc/pki/nssdb * CAfile: /etc/pki/tls/certs/ca-bundle.crt CApath: none * SSL connection using TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 * Server certificate: * subject: CN=ssl371609.cloudflaressl.com,OU=PositiveSSL Multi-Domain,OU=Domain Control Validated * start date: Nov 07 00:00:00 2019 GMT * expire date: May 15 23:59:59 2020 GMT * common name: ssl371609.cloudflaressl.com * issuer: CN=COMODO ECC Domain Validation Secure Server CA 2,O=COMODO CA Limited,L=Salford,ST=Greater Manchester,C=GB > GET / HTTP/1.1 > User-Agent: curl/7.29.0 > Host: geekflare.com > Accept: */* > < HTTP/1.1 200 OK < Date: Sat, 09 Nov 2019 19:41:37 GMT < Content-Type: text/html; charset=UTF-8 < Transfer-Encoding: chunked < Connection: keep-alive < Set-Cookie: __cfduid=d2ce6cd359ebc0b6eb5ff3a454ed042021573328497; expires=Sun, 08-Nov-20 19:41:37 GMT; path=/; domain=.geekflare.com; HttpOnly; Secure < Vary: Accept-Encoding < Link: <https://geekflare.com/wp-json/>; rel="https://api.w.org/" < Link: <https://geekflare.com/>; rel=shortlink < X-SRCache-Fetch-Status: HIT < X-SRCache-Store-Status: BYPASS < X-Frame-Options: SAMEORIGIN < X-Powered-By: EasyEngine v4.0.12 < Via: 1.1 google < CF-Cache-Status: DYNAMIC < Strict-Transport-Security: max-age=15552000; preload < X-Content-Type-Options: nosniff < Alt-Svc: h3-23=":443"; ma=86400 < Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" < Server: cloudflare < CF-RAY: 533243e4bcd4bbf4-LHR <
制限速度でダウンロード
最適化に取り組んでいて、特定の速度でダウンロードするのにかかる時間を知りたい場合は、次のことができます:-
curl –-limit-rate 2000B
元:
curl –-limit-rate 2000B https://gf.dev
プロキシを使用して接続する
プロキシを使用して外部世界に接続する必要がある DMZ サーバーで作業している場合に非常に便利です。
curl --proxy yourproxy:port https://yoururl.com
ヘッダーを挿入して URL をテストする
特定の問題をテストまたはトラブルシューティングするために、データにヘッダーを挿入することで curl を使用できます。 Content-Type でリクエストする次の例を見てみましょう。

curl --header 'Content-Type: application/json' http://yoururl.com
上記を実行することで、リクエスト ヘッダーで Content-Type を application/json として渡すように curl に要求しています。
応答ヘッダーのみを表示
トラブルシューティングを行っていて、すぐに応答ヘッダーを確認したい場合は、次の構文を使用できます。
curl --head http://yoururl.com
元:
[root@gf-lab tmp]# curl --head https://chandan.io HTTP/1.1 200 OK Date: Sat, 09 Nov 2019 19:51:23 GMT Content-Type: text/html Connection: keep-alive Set-Cookie: __cfduid=d3cb2c7b8e566ad99c870b0af12b0f1eb1573329083; expires=Sun, 08-Nov-20 19:51:23 GMT; path=/; domain=.chandan.io; HttpOnly X-GUploader-UploadID: AEnB2Uo96JhvJmR2zYUL-Ndh2ta3UD_ykQAB5C7O8cjZQhCf-GxHQ0MsodSzRnl3guSN3ywAYNjtWcPXfwDXjLg3bQ-P5vQMOA Expires: Sat, 09 Nov 2019 20:51:23 GMT Cache-Control: public, max-age=3600 Last-Modified: Mon, 06 Aug 2018 10:45:47 GMT x-goog-generation: 1533552347482034 x-goog-metageneration: 1 x-goog-stored-content-encoding: identity x-goog-stored-content-length: 24620 x-goog-hash: crc32c=DpDPAQ== x-goog-hash: md5=cIP/3rusdUx12Zla1kf1yA== x-goog-storage-class: MULTI_REGIONAL Accept-Ranges: bytes CF-Cache-Status: DYNAMIC Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" Server: cloudflare CF-RAY: 53325234dc2fbb9a-LHR [root@gf-lab tmp]#
HTTPS/SSL URL に接続し、SSL 証明書エラーを無視する
SSL/TLS 証明書で保護された URL にアクセスしようとして、その証明書が間違っているか、CN が一致しない場合、次のエラーが発生します。
curl: (51) Unable to communicate securely with peer: requested domain name does not match the server's certificate.
--insecure
フラグを使用して、証明書エラーを無視するように cURL に指示できます。
curl --insecure https://yoururl.com
特定のプロトコル (SSL/TLS) を使用して接続する
特定の URL が特定の SSL/TLS プロトコルでハンドシェイクできるかどうかをテストするのに非常に便利です。
SSL v3 を使用して接続するには
curl --sslv3 https://yoururl.com
異なる TLS バージョンの場合
curl --tlsv1 https://example.com curl --tlsv1.0 https://example.com curl --tlsv1.1 https://example.com curl --tlsv1.2 https://example.com curl --tlsv1.3 https://example.com
FTPサーバーからファイルをダウンロード
ユーザー名とパスワードを指定して、curl を使用してファイルをダウンロードすることもできます。
curl -u user:password -O ftp://ftpurl/style.css
「 -v 」を任意の構文でいつでも使用して、冗長モードで出力できます。
ホスト ヘッダーの使用
ホスト ヘッダーは、ホスト ヘッダーが一致する場合にのみ要求されたコンテンツを利用できる場合に、IP 経由でターゲット URL をテストするのに役立ちます。 または、ロード バランサーの IP/URL を使用してアプリケーションをテストする場合。
curl --header 'Host: targetapplication.com' https://192.0.0.1:8080/
cURL をオンラインで使用するのはどうですか?
はい、以下のツールで可能です。 cURL をリモートで実行できます。
オンライン CURL – URL をオンラインで取得するための軽量ツールで、次のオプションを追加できます。
--connect-timeout --cookie --data --header --head --location --max-time --proxy --request --user --url --user-agent
cURL コマンド ライン ビルダー – これは異なります。 素敵な UI で情報を入力できる curl コマンドを作成するのに役立ち、下部に cURL コマンドが表示されます。

cURL は、リアルタイム接続の問題をトラブルシューティングするための便利なユーティリティです。上記がお役に立てば幸いです。 詳細を知りたい場合は、Linux Command Line Basics オンライン コースをお勧めします。