はじめに
Linux環境で開発をしていると、「Webページを取得する」「APIを叩く」といったシーンでcurlコマンドを使うことがあります。
ですが、私は頻繁に使うコマンドではないので、幾つかの機能(オプション)を備忘録としてまとめてみました。
代表的なオプション
オプション | 意味 |
---|---|
-o | 指定したファイル名で保存する。 |
-O | 転送元と同じファイル名で保存する。 |
--create-dirs | 指定したフォルダが存在しない時に、自動的にフォルダを作成してからファイルとして保存する。 |
-# | プログレスバーを表示する。 |
-I | ヘッダ情報を表示する。 |
-i | ヘッダ情報を付けてページを表示する。 |
コマンドの利用例
- 私が一番よく使うのは「オプションなし」のシンプルなcurlコマンドです。
- ISOイメージなど大きなファイルをダウンロードする際には、"-#"オプションを付けてプログレスバーを表示させると、進行状況を確認できて良さそうです。
[nkojima@kumotori ~]$ # オプションなしで実行する場合
[nkojima@kumotori ~]$ curl https://qiita.com/nkojima
<!DOCTYPE html><html xmlns:og="http://ogp.me/ns#"><head><meta charset="UTF-8" /><title>nkojima - Qiita</title><meta content="width=device-width,init
ial-scale=1" name="viewport" /><meta content="nkojima's profile. Let's see nkojima's posts." name="description" />
...
[nkojima@kumotori ~]$ # 指定したファイル名で保存する。
[nkojima@kumotori ~]$ curl https://qiita.com/nkojima -o qiita.html
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 39134 0 39134 0 0 51056 0 --:--:-- --:--:-- --:--:-- 67940
[nkojima@kumotori ~]$ # 転送元と同じファイル名で保存する。保管先フォルダが無ければ新規作成する。
[nkojima@kumotori ~]$ curl https://qiita.com/nkojima -o ./downloads/qiita.html --create-dirs
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 39134 0 39134 0 0 111k 0 --:--:-- --:--:-- --:--:-- 278k
[nkojima@kumotori ~]$ # プログレスバーを表示する。
[nkojima@kumotori ~]$ curl http://ftp.jaist.ac.jp/pub/Linux/CentOS/7.6.1810/isos/x86_64/CentOS-7-x86_64-DVD-1810.iso -O -#
#################### 28.3%
[nkojima@kumotori ~]$ # ヘッダ情報を表示する。
[nkojima@kumotori ~]$ curl https://www.google.co.jp -I
HTTP/1.1 200 OK
Date: Sat, 02 Feb 2019 09:26:13 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=Shift_JIS
P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Set-Cookie: 1P_JAR=2019-02-02-09; expires=Mon, 04-Mar-2019 09:26:13 GMT; path=/; domain=.google.co.jp
Set-Cookie: NID=158=egLwtfuJLqVmt7PCQa7FAqw_v0gbZFTG93iSJ7y5txneEuxwFVRxiqtf21B24wjilHCSsQW29IcOXNtRjc5unznmWFsvdgveWeAZm6LsmdYMmh_pTH-1eCsKrEhby87f4zH8qrxmtQT9ZVK53SAVnT1L5ByA9_tVkj4vGlKcQ1I; expires=Sun, 04-Aug-2019 09:26:13 GMT; path=/; domain=.google.co.jp; HttpOnly
Transfer-Encoding: chunked
Alt-Svc: quic=":443"; ma=2592000; v="44,43,39"
Accept-Ranges: none
Vary: Accept-Encoding
応用編:jqコマンドを併用する
- 例えば、APIを叩いて得られた結果がJSONの場合、"jq"というコマンドを併用すると結果がとても見やすくなります。
- "jq"コマンドの本家サイトを見ると、「軽量で柔軟なJSON処理プログラム」と書かれています。
事前にjqをインストールする
[root@kumotori ~]# yum install epel-release -y
[root@kumotori ~]# yum install jq -y
jqを使わない場合
[root@kumotori ~]# curl http://weather.livedoor.com/forecast/webservice/json/v1?city=130010 -s
{"pinpointLocations":[{"link":"http://weather.livedoor.com/area/forecast/1310100","name":"\u5343\u4ee3\u7530\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1310200","name":"\u4e2d\u592e\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1310300","name":"\u6e2f\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1310400","name":"\u65b0\u5bbf\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1310500","name":"\u6587\u4eac\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1310600","name":"\u53f0\u6771\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1310700","name":"\u58a8\u7530\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1310800","name":"\u6c5f\u6771\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1310900","name":"\u54c1\u5ddd\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1311000","name":"\u76ee\u9ed2\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1311100","name":"\u5927\u7530\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1311200","name":"\u4e16\u7530\u8c37\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1311300","name":"\u6e0b\u8c37\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1311400","name":"\u4e2d\u91ce\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1311500","name":"\u6749\u4e26\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1311600","name":"\u8c4a\u5cf6\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1311700","name":"\u5317\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1311800","name":"\u8352\u5ddd\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1311900","name":"\u677f\u6a4b\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1312000","name":"\u7df4\u99ac\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1312100","name":"\u8db3\u7acb\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1312200","name":"\u845b\u98fe\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1312300","name":"\u6c5f\u6238\u5ddd\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1320100","name":"\u516b\u738b\u5b50\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1320200","name":"\u7acb\u5ddd\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1320300","name":"\u6b66\u8535\u91ce\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1320400","name":"\u4e09\u9df9\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1320500","name":"\u9752\u6885\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1320600","name":"\u5e9c\u4e2d\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1320700","name":"\u662d\u5cf6\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1320800","name":"\u8abf\u5e03\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1320900","name":"\u753a\u7530\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1321000","name":"\u5c0f\u91d1\u4e95\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1321100","name":"\u5c0f\u5e73\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1321200","name":"\u65e5\u91ce\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1321300","name":"\u6771\u6751\u5c71\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1321400","name":"\u56fd\u5206\u5bfa\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1321500","name":"\u56fd\u7acb\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1321800","name":"\u798f\u751f\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1321900","name":"\u72db\u6c5f\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1322000","name":"\u6771\u5927\u548c\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1322100","name":"\u6e05\u702c\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1322200","name":"\u6771\u4e45\u7559\u7c73\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1322300","name":"\u6b66\u8535\u6751\u5c71\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1322400","name":"\u591a\u6469\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1322500","name":"\u7a32\u57ce\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1322700","name":"\u7fbd\u6751\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1322800","name":"\u3042\u304d\u308b\u91ce\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1322900","name":"\u897f\u6771\u4eac\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1330300","name":"\u745e\u7a42\u753a"},{"link":"http://weather.livedoor.com/area/forecast/1330500","name":"\u65e5\u306e\u51fa\u753a"},{"link":"http://weather.livedoor.com/area/forecast/1330700","name":"\u6a9c\u539f\u6751"},{"link":"http://weather.livedoor.com/area/forecast/1330800","name":"\u5965\u591a\u6469\u753a"}],"link":"http://weather.livedoor.com/area/forecast/130010","forecasts":[{"dateLabel":"\u4eca\u65e5","telop":"\u6674\u306e\u3061\u66c7","date":"2019-02-03","temperature":{"min":{"celsius":"1","fahrenheit":"33.8"},"max":{"celsius":"14","fahrenheit":"57.2"}},"image":{"width":50,"url":"http://weather.livedoor.com/img/icon/5.gif","title":"\u6674\u306e\u3061\u66c7","height":31}},{"dateLabel":"\u660e\u65e5","telop":"\u66c7\u306e\u3061\u6674","date":"2019-02-04","temperature":{"min":null,"max":null},"image":{"width":50,"url":"http://weather.livedoor.com/img/icon/12.gif","title":"\u66c7\u306e\u3061\u6674","height":31}}],"location":{"city":"\u6771\u4eac","area":"\u95a2\u6771","prefecture":"\u6771\u4eac\u90fd"},"publicTime":"2019-02-02T22:00:00\u002b0900","copyright":{"provider":[{"link":"http://tenki.jp/","name":"\u65e5\u672c\u6c17\u8c61\u5354\u4f1a"}],"link":"http://weather.livedoor.com/","title":"(C) LINE Corporation","image":{"width":118,"link":"http://weather.livedoor.com/","url":"http://weather.livedoor.com/img/cmn/livedoor.gif","title":"livedoor \u5929\u6c17\u60c5\u5831","height":26}},"title":"\u6771\u4eac\u90fd \u6771\u4eac \u306e\u5929\u6c17","description":{"text":" \u9ad8\u6c17\u5727\u304c\u3001\u65e5\u672c\u306e\u5357\u3092\u6771\u3078\u79fb\u52d5\u3057\u3066\u3044\u307e\u3059\u3002\n\n\u3010\u95a2\u6771\u7532\u4fe1\u5730\u65b9\u3011\n \u95a2\u6771\u7532\u4fe1\u5730\u65b9\u306f\u3001\u304a\u304a\u3080\u306d\u6674\u308c\u3066\u3044\u307e\u3059\u3002\n\n 2\u65e5\u306f\u3001\u9ad8\u6c17\u5727\u306b\u8986\u308f\u308c\u308b\u305f\u3081\u3001\u304a\u304a\u3080\u306d\u6674\u308c\u308b\u3067\u3057\u3087\u3046\u3002 \n \n 3\u65e5\u306f\u3001\u9ad8\u6c17\u5727\u306b\u8986\u308f\u308c\u3066\u6674\u308c\u307e\u3059\u304c\u3001\u65e5\u672c\u6d77\u3092\u6771\u5317\u6771\u306b\u9032\u3080\u4f4e\u6c17\u5727\u306e\u5f71\n\u97ff\u3067\u5348\u5f8c\u306f\u6b21\u7b2c\u306b\u66c7\u308a\u3068\u306a\u308a\u3001\u7532\u4fe1\u5730\u65b9\u3092\u4e2d\u5fc3\u306b\u96e8\u306e\u964d\u308b\u6240\u304c\u3042\u308b\u898b\u8fbc\u307f\u3067\n\u3059\u3002\n\n \u95a2\u6771\u8fd1\u6d77\u3067\u306f\u30012\u65e5\u304b\u30893\u65e5\u306b\u304b\u3051\u3066\u6ce2\u304c\u9ad8\u304f\u30012\u65e5\u306f\u3046\u306d\u308a\u3092\u4f34\u3046\u3067\u3057\n\u3087\u3046\u3002\u8239\u8236\u306f\u9ad8\u6ce2\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\n\n\u3010\u6771\u4eac\u5730\u65b9\u3011\n 2\u65e5\u306f\u3001\u304a\u304a\u3080\u306d\u6674\u308c\u308b\u3067\u3057\u3087\u3046\u3002\n 3\u65e5\u306f\u3001\u6674\u308c\u3067\u3001\u5915\u65b9\u304b\u3089\u66c7\u308a\u3068\u306a\u308b\u898b\u8fbc\u307f\u3067\u3059\u3002","publicTime":"2019-02-02T22:22:00\u002b0900"}}
jqを併用した場合
[root@kumotori ~]# curl http://weather.livedoor.com/forecast/webservice/json/v1?city=130010 -s | jq "."
{
"description": {
"publicTime": "2019-02-02T22:22:00+0900",
"text": " 高気圧が、日本の南を東へ移動しています。\n\n【関東甲信地方】\n 関東甲信地方は、おおむね晴れています。\n\n 2日は、高気圧に覆われるため
、おおむね晴れるでしょう。 \n \n 3日は、高気圧に覆われて晴れますが、日本海を東北東に進む低気圧の影\n響で午後は次第に曇りとなり、甲信地方を中心に雨の
降る所がある見込みで\nす。\n\n 関東近海では、2日から3日にかけて波が高く、2日はうねりを伴うでし\nょう。船舶は高波に注意してください。\n\n【東京地方】
\n 2日は、おおむね晴れるでしょう。\n 3日は、晴れで、夕方から曇りとなる見込みです。"
},
"title": "東京都 東京 の天気",
"copyright": {
"image": {
"height": 26,
"title": "livedoor 天気情報",
"url": "http://weather.livedoor.com/img/cmn/livedoor.gif",
"link": "http://weather.livedoor.com/",
"width": 118
},
"title": "(C) LINE Corporation",
"link": "http://weather.livedoor.com/",
"provider": [
{
"name": "日本気象協会",
"link": "http://tenki.jp/"
}
]
},
"publicTime": "2019-02-02T22:00:00+0900",
"location": {
"prefecture": "東京都",
"area": "関東",
"city": "東京"
},
"forecasts": [
{
"image": {
"height": 31,
"title": "晴のち曇",
"url": "http://weather.livedoor.com/img/icon/5.gif",
"width": 50
},
"temperature": {
"max": {
"fahrenheit": "57.2",
"celsius": "14"
},
"min": {
"fahrenheit": "33.8",
"celsius": "1"
}
},
"date": "2019-02-03",
"telop": "晴のち曇",
"dateLabel": "今日"
},
{
"image": {
"height": 31,
"title": "曇のち晴",
"url": "http://weather.livedoor.com/img/icon/12.gif",
"width": 50
},
"temperature": {
"max": null,
"min": null
},
"date": "2019-02-04",
"telop": "曇のち晴",
"dateLabel": "明日"
}
],
"link": "http://weather.livedoor.com/area/forecast/130010",
"pinpointLocations": [
{
"name": "千代田区",
"link": "http://weather.livedoor.com/area/forecast/1310100"
},
{
"name": "中央区",
"link": "http://weather.livedoor.com/area/forecast/1310200"
},
{
"name": "港区",
"link": "http://weather.livedoor.com/area/forecast/1310300"
},
{
"name": "新宿区",
"link": "http://weather.livedoor.com/area/forecast/1310400"
},
{
"name": "文京区",
"link": "http://weather.livedoor.com/area/forecast/1310500"
},
{
"name": "台東区",
"link": "http://weather.livedoor.com/area/forecast/1310600"
},
{
"name": "墨田区",
"link": "http://weather.livedoor.com/area/forecast/1310700"
},
{
"name": "江東区",
"link": "http://weather.livedoor.com/area/forecast/1310800"
},
{
"name": "品川区",
"link": "http://weather.livedoor.com/area/forecast/1310900"
},
{
"name": "目黒区",
"link": "http://weather.livedoor.com/area/forecast/1311000"
},
{
"name": "大田区",
"link": "http://weather.livedoor.com/area/forecast/1311100"
},
{
"name": "世田谷区",
"link": "http://weather.livedoor.com/area/forecast/1311200"
},
{
"name": "渋谷区",
"link": "http://weather.livedoor.com/area/forecast/1311300"
},
{
"name": "中野区",
"link": "http://weather.livedoor.com/area/forecast/1311400"
},
{
"name": "杉並区",
"link": "http://weather.livedoor.com/area/forecast/1311500"
},
{
"name": "豊島区",
"link": "http://weather.livedoor.com/area/forecast/1311600"
},
{
"name": "北区",
"link": "http://weather.livedoor.com/area/forecast/1311700"
},
{
"name": "荒川区",
"link": "http://weather.livedoor.com/area/forecast/1311800"
},
{
"name": "板橋区",
"link": "http://weather.livedoor.com/area/forecast/1311900"
},
{
"name": "練馬区",
"link": "http://weather.livedoor.com/area/forecast/1312000"
},
{
"name": "足立区",
"link": "http://weather.livedoor.com/area/forecast/1312100"
},
{
"name": "葛飾区",
"link": "http://weather.livedoor.com/area/forecast/1312200"
},
{
"name": "江戸川区",
"link": "http://weather.livedoor.com/area/forecast/1312300"
},
{
"name": "八王子市",
"link": "http://weather.livedoor.com/area/forecast/1320100"
},
{
"name": "立川市",
"link": "http://weather.livedoor.com/area/forecast/1320200"
},
{
"name": "武蔵野市",
"link": "http://weather.livedoor.com/area/forecast/1320300"
},
{
"name": "三鷹市",
"link": "http://weather.livedoor.com/area/forecast/1320400"
},
{
"name": "青梅市",
"link": "http://weather.livedoor.com/area/forecast/1320500"
},
{
"name": "府中市",
"link": "http://weather.livedoor.com/area/forecast/1320600"
},
{
"name": "昭島市",
"link": "http://weather.livedoor.com/area/forecast/1320700"
},
{
"name": "調布市",
"link": "http://weather.livedoor.com/area/forecast/1320800"
},
{
"name": "町田市",
"link": "http://weather.livedoor.com/area/forecast/1320900"
},
{
"name": "小金井市",
"link": "http://weather.livedoor.com/area/forecast/1321000"
},
{
"name": "小平市",
"link": "http://weather.livedoor.com/area/forecast/1321100"
},
{
"name": "日野市",
"link": "http://weather.livedoor.com/area/forecast/1321200"
},
{
"name": "東村山市",
"link": "http://weather.livedoor.com/area/forecast/1321300"
},
{
"name": "国分寺市",
"link": "http://weather.livedoor.com/area/forecast/1321400"
},
{
"name": "国立市",
"link": "http://weather.livedoor.com/area/forecast/1321500"
},
{
"name": "福生市",
"link": "http://weather.livedoor.com/area/forecast/1321800"
},
{
"name": "狛江市",
"link": "http://weather.livedoor.com/area/forecast/1321900"
},
{
"name": "東大和市",
"link": "http://weather.livedoor.com/area/forecast/1322000"
},
{
"name": "清瀬市",
"link": "http://weather.livedoor.com/area/forecast/1322100"
},
{
"name": "東久留米市",
"link": "http://weather.livedoor.com/area/forecast/1322200"
},
{
"name": "武蔵村山市",
"link": "http://weather.livedoor.com/area/forecast/1322300"
},
{
"name": "多摩市",
"link": "http://weather.livedoor.com/area/forecast/1322400"
},
{
"name": "稲城市",
"link": "http://weather.livedoor.com/area/forecast/1322500"
},
{
"name": "羽村市",
"link": "http://weather.livedoor.com/area/forecast/1322700"
},
{
"name": "あきる野市",
"link": "http://weather.livedoor.com/area/forecast/1322800"
},
{
"name": "西東京市",
"link": "http://weather.livedoor.com/area/forecast/1322900"
},
{
"name": "瑞穂町",
"link": "http://weather.livedoor.com/area/forecast/1330300"
},
{
"name": "日の出町",
"link": "http://weather.livedoor.com/area/forecast/1330500"
},
{
"name": "檜原村",
"link": "http://weather.livedoor.com/area/forecast/1330700"
},
{
"name": "奥多摩町",
"link": "http://weather.livedoor.com/area/forecast/1330800"
}
]
}