知っておくとちょっと便利!curl コマンドの使い方をご紹介

★★★ お知らせ ★★★

◆ 6月のPS Liveは決済サービスStripeのデモ!◆
決済サービスのStripeでサブスクリプションの支払い機能を作ってみた
Stripeの機能の一部を切り出して、簡単なサブスクリプションを作るとどうなるのかを、デモをまじえてご紹介します。
⇒ 詳細はこちらから

◆【セミナー開催】 可視化ツールGrafana~初めてのダッシュボード作成◆
デモンストレーション付き!
本セミナーでは、売上データを用いて、4種類のパネル作成方法をお見せします。
⇒ お申込みはこちらから

◆ エンジニア業務環境アンケート結果 ◆
エンジニアが自分の働き方にどういったことで満足していて、不満を感じているのか、働きたい会社像として何を求めているのか、業務環境調査を実施しました。ぜひご覧ください。
⇒ アンケート結果はこちらから

今号では、curl コマンドの使い方やオプションについてご紹介します!

curl コマンドとは

curl コマンドは、様々な通信プロトコルでデータの送受信を行うことができるコマンドです。
よく使われる方法としては、Web サイトへ http リクエストを送信してコンテンツを表示する、等があります。
また様々なオプションを指定する事で、データ取得時の条件や、取得する情報を変える事ができます。

基本の書式

curl コマンドの基本の書式は以下の通りです。
http リクエストの場合ですと、以下の様に Web サイトのコンテンツがテキストで表示されます。

$ curl http://example.com
This is example index page

また、コンマ (‘) や {} [] を使用する事で、アクセスする URL の範囲指定できます。

$ curl 'http://example[1-3].com'

[1/3]: http://example1.com --> 
--_curl_--http://example1.com
This is example1 index page

[2/3]: http://example2.com --> 
--_curl_--http://example2.com
This is example2 index page

[3/3]: http://example3.com --> 
--_curl_--http://example3.com
This is example3 index page
$ curl 'http://example-{one,two,three}.com'

[1/3]: http://example-one.com --> 
--_curl_--http://example-one.com
This is example-one index page

[2/3]: http://example-two.com --> 
--_curl_--http://example-two.com
This is example-two index page

[3/3]: http://example-three.com --> 
--_curl_--http://example-three.com
This is example-three index page

curl コマンドのオプション

次に、curl コマンドで使用可能なオプションをご紹介します。
オプションには多くの種類がありますが、よく使用されると考えられるものを抜粋してご紹介します。

  • コンテンツをファイル出力する
    -o オプションを指定すると、取得したコンテンツをファイル出力します。

    $ curl http://example.com -o examplefile
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100    27  100    27    0     0   3879      0 --:--:-- --:--:-- --:--:--  4500
    $ cat examplefile
    This is example index page
    

    ※examplefile は、任意のファイル名に置き換えてください。 -o オプションに併せて -s オプションを指定すると、コンテンツ取得時の進捗状況を非表示にします。

    $ curl http://example.com -s -o examplefile
    $ cat examplefile
    This is example index page
    
  • SSL 接続による証明書エラーを無視する
    自己署名証明書等、不正な証明書を使用するとエラーになりコンテンツが表示できない場合があります。
    このエラーを無視してコンテンツを表示したい場合は -k オプションを指定します。

    # -kオプションがない場合
    $curl https://example.com
    curl: (60) Peer's certificate issuer has been marked as not trusted by the user.
    More details here: http://curl.haxx.se/docs/sslcerts.html
    
    curl performs SSL certificate verification by default, using a "bundle"
     of Certificate Authority (CA) public keys (CA certs). If the default
     bundle file isn't adequate, you can specify an alternate file
     using the --cacert option.
    If this HTTPS server uses a certificate signed by a CA represented in
     the bundle, the certificate verification probably failed due to a
     problem with the certificate (it might be expired, or the name might
     not match the domain name in the URL).
    If you'd like to turn off curl's verification of the certificate, use
     the -k (or --insecure) option.
    
    # -kオプションがある場合
    $ curl -k https://example.com
    This is example index page
    
  • プロキシサーバを指定してアクセスする
    -x オプションを指定すると、プロキシサーバ経由でアクセスします。

    $ curl -x http://proxy-example.com:3128 http://example.com
    …
    

    ※proxy-example.com:3128 は、任意の <プロキシサーバ>:<ポート番号> に置き換えてください。

  • コンテンツ取得時の詳細なログを表示する
    -v オプションを指定すると、コンテンツを取得した際の詳細なログ (レスポンスヘッダの内容や HTTP ステータスコード等) を表示します。

    $ curl -v http://example.com
    * About to connect() to example.com port 80 (#0)
    *   Trying 127.0.0.1...
    * Connected to example.com (127.0.0.1) port 80 (#0)
    > GET / HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: example.com
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Date: Wed, 20 Jul 2022 20:12:42 GMT
    < Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips
    < Last-Modified: Wed, 20 Jul 2022 19:15:01 GMT
    < ETag: "1b-5e4416af2a4ab"
    < Accept-Ranges: bytes
    < Content-Length: 27
    < Content-Type: text/html; charset=UTF-8
    <
    This is example index page
    * Connection #0 to host example.com left intact
    
  • HTTP レスポンスヘッダを表示する
    -I オプションを指定すると、コンテンツを取得した際の HTTP レスポンスヘッダの内容を表示します。

    $ curl -I http://example.com
    HTTP/1.1 200 OK
    Date: Wed, 20 Jul 2022 20:14:49 GMT
    Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips
    Last-Modified: Wed, 20 Jul 2022 19:15:01 GMT
    ETag: "1b-5e4416af2a4ab"
    Accept-Ranges: bytes
    Content-Length: 27
    Content-Type: text/html; charset=UTF-8
    
アバター画像
About 貝野 友香 49 Articles
OSSよろず相談室でサポートをやっています。時々ライブ配信や勉強会に出ていることもあります。
ご覧いただきありがとうございます! この投稿はお役に立ちましたか?

役に立った 役に立たなかった

4人がこの投稿は役に立ったと言っています。


ご覧いただきありがとうございます。
ブログの最新情報はSNSでも発信しております。
ぜひTwitterのフォロー&Facebookページにいいねをお願い致します!



>> 雑誌等の執筆依頼を受付しております。
   ご希望の方はお気軽にお問い合わせください!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


質問はこちら 閉じる