- 追加された行はこの色です。
- 削除された行はこの色です。
// 下階層用テンプレート
#topicpath
----
//ここにコンテンツを記述します。
#contents
**使い方メモ [#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¶m2=v2をGETでリクエストするケース
curl 'http://xxx.example.com/query?param1=v1¶m2=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サーバはこんな感じ。
ちなみにうける側のWEBサーバ(nodejs)はこんな感じ。
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/');
*** ncでWEBサーバ [#te306602]
話がどんどん脱線していきますが、ncコマンドがはいってるLinux系なら(Macもはいってました)
[[nc コマンドで簡易HTTPサーバ - sonots:blog>http://blog.livedoor.jp/sonots/archives/34703829.html]]
ここにあるワンライナー:
bash -c 'trap exit INT; while true; do ( echo "HTTP/1.0 200 Ok"; echo; echo "Hello World" ) | nc -l 8080; done'
でWebサーバが立ち上げられます。さくっとPOSTの確認したい時とかに便利ですね。
**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しないで送信しています。
** ファイルのデータを送信する [#ta1965ed]
$ cat sample.json
{"param1": "value1",
"param2": "value2"}
$ curl --data-binary @sample.json http://localhost:8082
でファイルのデータを送信出来ます。 --dataだと改行が無視されるので、バイナリで送信しています。
** 標準出力の結果をパイプで渡す [#e1f93132]
$ cat sample.json | curl --data-binary @- localhost:8082
こんな風にしてもよいみたい。なので
$ cat << __EOF__ | curl --data-binary @- localhost:8082
とすればその場で入力した文字列を送信することができますね。
**関連リンク [#d4a7a56a]
-[[curl コマンド 使い方メモ - Qiita>http://qiita.com/yasuhiroki/items/a569d3371a66e365316f]]
-[[curl コマンドで、特定の(ヘッダ)情報だけを取り出す。レスポンスヘッダ - それマグで!>http://takuya-1st.hatenablog.jp/entry/2014/09/23/225817]]
-[[とりあえず覚えた cURL のオプション - いちろぐ>http://ichiroku11.hatenablog.jp/entry/2014/03/31/232758]]
----
この記事は
#vote(おもしろかった,そうでもない)
#comment
#topicpath
SIZE(10){現在のアクセス:&counter;}