curl
ネットワーク
書き方
curl [options] <url>URL を一つ取ってきて本文を画面に出します。ブラウザーと違い `-L` なしでは転送を追わないので、301 が返ると空の応答のように見えます。
よく使うオプション
| オプション | 意味 |
|---|---|
| -L | Follow redirects. curl does not follow them on its own, unlike a browser. |
| -i | Print the response headers along with the body; -I sends HEAD and prints headers only. |
| -d '{"a":1}' | Send a request body. This alone already means POST. |
| -H "Content-Type: application/json" | Add a request header; repeat -H for more. |
| -X PUT | Override the method. Unnecessary with -d, which is already a POST. |
| -o <file> / -O | Save the body to a file / save it under the remote filename. |
| -s / -sS | No progress meter / silent but still print errors. |
| -f | Exit nonzero on HTTP 4xx and 5xx instead of printing the error page. |
| -k | Skip certificate verification. Debugging only, never in production. |
例
curl -fsSL https://example.com/install.sh -o install.shDownload quietly, follow redirects, fail loudly on a 404.
curl -i -H "Content-Type: application/json" -d '{"name":"a"}' https://api.example.com/itemsPOST JSON and read the response headers.
curl -s -o /dev/null -w "%{http_code}\n" https://example.comPrint just the status code.
つながらないとき、どこまで行けたのかを絞ります — 名前が引けないのか、経路がないのか、ポートが閉じているのか。
読み方
- 角括弧 [ ] は省いてよい部分です。
- 三点 … は複数並べられるという意味です。
- オプションは大文字小文字を区別します — -r と -R が別物のコマンドもあります。
よくある質問
Q. curl は何をしますか。
URL を一つ取ってきて本文を画面に出します。ブラウザーと違い `-L` なしでは転送を追わないので、301 が返ると空の応答のように見えます。
Q. どう打ちますか。
curl [options] <url> — 角括弧は省いてよい部分です。
Q. よく使うオプションはいくつですか。
ここにまとめたのは9個です。全部は man curl にあります。このコマンドはネットワークの仲間です。
関連するコマンド
man ページ: man curl