使い方メモ

curl https://example.com/temperature_130010.json // 通常
curl https://example.com/temperature_130010.json  -o result.log //戻り電文をファイル出力
curl https://example.com/temperature_130010.json  -O  //戻り電文をファイル出力
curl https://example.com/temperature_130010.json  -s // ゲージを出さない(エラーもでない)
curl https://example.com/temperature_130010.json  -Ss // ゲージを出さない(エラーは出しつつ)。
curl https://example.com/temperature_130010.json  -I // レスポンスヘッダを出す
curl https://example.com/temperature_130010.json  -i // ヘッダ、Bodyを出す
curl https://example.com/temperature_130010.json  -v // リクエストヘッダを出す
curl https://example.com/temperature_130010.json --trace temperature_130010.log //traceのログをとる1
curl https://example.com/temperature_130010.json --trace-ascii temperature_130010_ascii.log //traceのログをとる2
curl https://example.com/temperature_130010.json --trace-ascii temperature_130010_time.log --trace-time  //traceのログをとる(日時つき)

Key/valueでGET

いわゆる param1=v1&param2=v2をGETでリクエストするケース

curl 'http://xxx.example.com/query?param1=v1&param2=v2'

と''で囲むか

curl http://xxx.example.com/query -d 'param1=v1' -d 'param2=v2' -G

と-GでGETを明示してパラメタを指定するか

パラメタが一つの場合は、

curl http://xxx.example.com/query?param1=v1

と素直にやればOK

Key/valueでPOST

curl -X POST --data-urlencode 
'payload={"channel": "#general", "username": "webhookbot", 
"text": "This is posted to #general and comes from a bot named webhookbot.", 
"icon_emoji": ":ghost:"}' 
https://hooks.slack.com/services/xxxx/xxxx/xxxxx

たとえば、

curl 127.0.0.1:9000 --data-urlencode 'k1=値1' --data-urlencode 'k2=値2' 

は、下記のhtmlのPOSTとおなじ。

<!DOCTYPE html>
<html>
<body>

<form action="http://127.0.0.1:9000" method="post" accept-charset="utf-8">
  <input name="k1" value="値1" />
  <input name="k2" value="値2" />
  <button>submit</button>
</form>
</body>
</html>

ちなみにうける側のWEBサーバはこんな感じ。

var http = require('http');
var url = require('url');

var server = http.createServer(
function (request, response) {
  if(request.method=='POST') {
    var body='';
    request.on('data', function (data) {
      body +=data;
    });
    request.on('end',function(){
      console.log(body);
    });
 } else if(request.method=='GET') {
    var url_parts = url.parse(request.url,true);
    console.log(url_parts.query);
    console.log(url_parts.query['k1']);
    console.log(url_parts.query['k2']);
}
 response.writeHead(200, {'Content-Type': 'text/plain'});
 response.write('Hello World!!\n');
 response.end();
}
).listen(9000);
console.log('Server running at http://127.0.0.1:9000/');

JSONをPOST

curl -X POST 127.0.0.1:9000 -v -H 'Content-Type:appliation/json' --data '{"key":"value"}'

デフォルトは、

Content-Type: application/x-www-form-urlencoded

なので -H でContent-Typeを変更します。またContent-TypeをJSONにしたのでデータはencodeしないで送信しています。

関連リンク


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS