curlは、コマンドラインからネットワークリクエストを作成できる素晴らしいツールです。
curlは、ネットワークを介してデータを転送できるコマンドラインツールです。
HTTP、HTTPS、FTP、FTPS、SFTP、IMAP、SMTP、POP3など、すぐに使用できる多くのプロトコルをサポートします。
ネットワーク要求のデバッグに関しては、curlは見つけることができる最高のツールの1つです。
これは、使用方法を知ったら、いつでも戻ってくるツールの1つです。プログラマーの親友。
それは普遍的で、Linux、Mac、Windowsで動作します。を参照してください公式インストールガイドシステムにインストールします。
おもしろい事実:カールの作者でありメンテナであるスウェーデン人は、彼の仕事(curlとlibcurl)がコンピューティングの世界に貢献したことでスウェーデン王から授与されました。
HTTPリクエストを処理するときに実行する可能性が最も高いコマンドと操作のいくつかを詳しく見ていきましょう。
これらの例には、最も一般的なプロトコルであるHTTPの使用が含まれます。
- HTTPGETリクエストを実行します
- HTTP応答ヘッダーを取得します
- HTTP応答ヘッダーのみを取得します
- HTTPPOSTリクエストを実行します
- JSONを送信するHTTPPOSTリクエストを実行します
- HTTPPUTリクエストを実行します
- リダイレクトに従う
- 応答をファイルに保存する
- HTTP認証の使用
- 別のユーザーエージェントを設定する
- リクエストとレスポンスのすべての詳細を検査する
- ブラウザネットワークリクエストをcurlコマンドにコピーする
HTTPGETリクエストを実行します
リクエストを実行すると、curlはレスポンスの本文を返します。
curl https://flaviocopes.com/
HTTP応答ヘッダーを取得します
デフォルトでは、応答ヘッダーはcurlの出力で非表示になっています。それらを表示するには、i
オプション:
curl -i https://flaviocopes.com/
HTTP応答ヘッダーのみを取得します
を使用してI
オプション、あなたは得ることができますのみ応答本文ではなくヘッダー:
curl -I https://flaviocopes.com/
HTTPPOSTリクエストを実行します
ザ・X
オプションを使用すると、使用するHTTPメソッドを変更できます。デフォルトでは、GETが使用され、書き込みと同じです。
curl -X GET https://flaviocopes.com/
使用する-X POST
POSTリクエストを実行します。
エンコードされたデータURLを渡すPOSTリクエストを実行できます。
curl -d "option=value&something=anothervalue" -X POST https://flaviocopes.com/
この場合、application/x-www-form-urlencoded
Content-Typeが送信されます。
JSONを送信するHTTPPOSTリクエストを実行します
上記の例のようにURLエンコードされたデータを投稿する代わりに、JSONを送信することをお勧めします。
この場合、を使用して、Content-Typeヘッダーを明示的に設定する必要があります。H
オプション:
curl -d '{"option": "value", "something": "anothervalue"}' -H "Content-Type: application/json" -X POST https://flaviocopes.com/
ディスクからJSONファイルを送信することもできます。
curl -d "@my-file.json" -X POST https://flaviocopes.com/
HTTPPUTリクエストを実行します
概念はPOSTリクエストの場合と同じですが、を使用してHTTPメソッドを変更するだけです。-X PUT
リダイレクトに従う
301のようなリダイレクト応答。Location
応答ヘッダーは、を指定することで自動的に続くことができますL
オプション:
curl http://flaviocopes.com/
リダイレクト先に設定したHTTPSバージョンに自動的には従いませんが、これは次のようになります。
curl -L http://flaviocopes.com/
応答をファイルに保存する
を使用してo
応答をファイルに保存するようにcurlに指示できるオプション:
curl -o file.html https://flaviocopes.com/
を使用して、ファイルをその名前でサーバーに保存することもできます。O
オプション:
curl -O https://flaviocopes.com/index.html
HTTP認証の使用
リソースに基本HTTP認証が必要な場合は、u
ユーザーを渡すオプション:パスワード値:
curl -u user:pass https://flaviocopes.com/
別のユーザーエージェントを設定する
ユーザーエージェントは、どのクライアントがリクエストを実行しているかをサーバーに通知します。デフォルトでは、curlはcurl/<version>
次のようなユーザーエージェント:curl/7.54.0
。
を使用して別のユーザーエージェントを指定できます--user-agent
オプション:
curl --user-agent "my-user-agent" https://flaviocopes.com
リクエストとレスポンスのすべての詳細を検査する
使用--verbose
curlにリクエストとレスポンスのすべての詳細を出力させるオプション:
curl --verbose -I https://flaviocopes.com/
* Trying 178.128.202.129...
* TCP_NODELAY set
* Connected to flaviocopes.com (178.128.202.129) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate: flaviocopes.com
* Server certificate: Let's Encrypt Authority X3
* Server certificate: DST Root CA X3
> HEAD / HTTP/1.1
> Host: flaviocopes.com
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Cache-Control: public, max-age=0, must-revalidate
Cache-Control: public, max-age=0, must-revalidate
< Content-Type: text/html; charset=UTF-8
Content-Type: text/html; charset=UTF-8
< Date: Mon, 30 Jul 2018 08:08:41 GMT
Date: Mon, 30 Jul 2018 08:08:41 GMT
...Copying any browser network request to a curl command
When inspecting any network request using the Chrome Developer Tools, you have the option to copy that request to a curl request:

curl 'https://github.com/curl/curl' -H 'Connection: keep-alive' -H 'Pragma: no-cache' -H 'Cache-Control: no-cache' -H 'Upgrade-Insecure-Requests: 1' -H 'DNT: 1' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'Referer: https://www.google.it/' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.9,it;q=0.8' -H 'Cookie: _octo=GH1.1.933116459.1507545550; _ga=GA1.2.643383860.1507545550; tz=Europe%2FRome; user_session=XXXXX; __Host-user_session_same_site=YYYYYY; dotcom_user=flaviocopes; logged_in=yes; has_recent_activity=1; _gh_sess=ZZZZZZ' --compressed
More network tutorials:
- Introduction to WebSockets
- How HTTP requests work
- The HTTP Request Headers List
- The HTTP Response Headers List
- HTTP vs HTTPS
- What is an RFC?
- The HTTP protocol
- The HTTPS protocol
- The curl guide to HTTP requests
- Caching in HTTP
- The HTTP Status Codes List
- What is a CDN?
- The HTTP/2 protocol
- What is a port
- DNS, Domain Name System
- The TCP Protocol
- The UDP Protocol
- An introduction to REST APIs
- How to install a local SSL certificate in macOS
- How to generate a local SSL certificate
- How to configure Nginx for HTTPS
- A simple nginx reverse proxy for serving multiple Node.js apps from subfolders
- What is a reverse proxy?