**使い方メモ [#md481544]
 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 [#h1bf53d1]
いわゆる 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 [#jd9fa1d6]
 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 [#l4bc9c64]
 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しないで送信しています。



**関連リンク [#d4a7a56a]
-[[curl コマンド 使い方メモ - Qiita>http://qiita.com/yasuhiroki/items/a569d3371a66e365316f]]
-[[curl コマンドで、特定の(ヘッダ)情報だけを取り出す。レスポンスヘッダ - それマグで!>http://takuya-1st.hatenablog.jp/entry/2014/09/23/225817]]

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