- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2021-12-14T02:34:55+00:00","","")
// 下階層用テンプレート
#topicpath
----
//ここにコンテンツを記述します。
#contents
** URLの一部を URL Encode [#xac7cfbd]
curl "http://localhost:5000/word/`echo '日本語' | nkf -WwMQ | tr = %`" | jq
で、パラメタでなくて、URLの一部をURL Encode できる
**使い方メモ [#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サーバ(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"}'
curl -X POST 127.0.0.1:9000 -v -H 'Content-Type:application/json' --data '{"key":"value"}'
デフォルトは、
Content-Type: application/x-www-form-urlencoded
なので -H でContent-Typeを変更します。
なので -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
とすればその場で入力した文字列を送信することができますね。
** リダイレクト先のデータを取得する。 [#s80f6d43]
よく、ダウンロードリンクをクリックするとHTTPレスポンスのリダイレクトを利用してダウンロードさせるケースがあったりします。リダイレクトに対応するには -Lを使います。
# curl -OL https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs921/ghostscript-9.21-linux-x86_64.tgz
** Oracle Javaを落とすサンプル [#bf24177e]
# curl -LO -b \
"oraclelicense=accept-securebackup-cookie" \
"http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz"
** curlと一緒に使われるJQのTIPS。ファイル出力 [#laa6410b]
$ cat << __EOF__ | jq . > result.json
> {"param1":"value1"}
> __EOF__
$ cat result.json
{
"param1": "value1"
}
** Proxyを通す件 [#k996808c]
curl -X POST \
-H 'Content-Type:application/json' \
-H 'Accept:application/json' \
--data-binary @parameter.json \
-k -x http://127.0.0.1:8888 \
https://labs.goo.ne.jp/api/entity
上記のように、-x オプションでプロキシを通す、プロキシを通すとよく発生するSSLエラーの回避のための -k オプションをつける、、などですね。ユーザ認証する場合は
-U userid:password
をつければよいっぽい。。
[[curlのプロキシ設定 - なみひらブログ>http://namihira.hatenablog.com/entry/2013/12/29/170839]]
[[curlコマンドでSSLのエラーを無視する>http://kaworu.jpn.org/kaworu/2011-02-11-1.php]]
** Requestbin サーバをDockerでたてる [#n1625752]
もはやcurlのTIPSでもなんでもないですが、
https://github.com/Runscope/requestbin
ネット上にあるRequestbinをDockerをつかって自前で立てる方法。。docker-composeが必要ですが、ラクです。
**関連リンク [#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(おもしろかった[4],そうでもない[0])
#comment
#topicpath
SIZE(10){現在のアクセス:&counter;}