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のログをとる(日時つき)
いわゆる 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
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サーバ(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コマンドがはいってるLinux系なら(Macもはいってました)
nc コマンドで簡易HTTPサーバ - sonots:blog
ここにあるワンライナー:
bash -c 'trap exit INT; while true; do ( echo "HTTP/1.0 200 Ok"; echo; echo "Hello World" ) | nc -l 8080; done'
でWebサーバが立ち上げられます。さくっと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しないで送信しています。
$ cat sample.json {"param1": "value1", "param2": "value2"} $ curl --data-binary @sample.json http://localhost:8082
でファイルのデータを送信出来ます。 --dataだと改行が無視されるので、バイナリで送信しています。
$ cat sample.json | curl --data-binary @- localhost:8082
こんな風にしてもよいみたい。なので
$ cat << __EOF__ | curl --data-binary @- localhost:8082
とすればその場で入力した文字列を送信することができますね。
よく、ダウンロードリンクをクリックするとHTTPレスポンスのリダイレクトを利用してダウンロードさせるケースがあったりします。リダイレクトに対応するには -Lを使います。
# curl -OL https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs921/ghostscript-9.21-linux-x86_64.tgz
# curl -LO -b \ "oraclelicense=accept-securebackup-cookie" \ "http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz"
この記事は
現在のアクセス:6975