LoginSignup
34
31

More than 5 years have passed since last update.

コマンドの出力をそのままcurlでPOSTする

Last updated at Posted at 2014-09-24

man page にこのように書いてある。

To read content from stdin instead of a file, use - as the filename. This goes for both @ and < constructs.

curlでファイル名を指定するのは --data @file のようにするので、ファイル名部分を - にするということは --data @- にするということだそうな。

--data だと改行がなくなってしまうので、--data-binary にすると標準出力そのまま送信できる。

echo "foo\nbar\nbaz" | curl --data-binary @- 'http://localhost:6666/'

どういうデータが送信されているかを見るにはncコマンドが便利

これで立ち上げておいて、

nc -l 6666

上のcurlコマンドを実行するとこのように出る。

POST / HTTP/1.1
User-Agent: curl/7.30.0
Host: localhost:6666
Accept: */*
Content-Length: 12
Content-Type: application/x-www-form-urlencoded

foo
bar
baz

--data だと改行されていないのはこれで見つけた。

34
31
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
31