LoginSignup
0
1

More than 5 years have passed since last update.

curl -F (--form) で @ から始まる文字列を POST する方法

Last updated at Posted at 2019-04-10

@ が先頭にあるとファイルとして認識されてしまう...

curl で -F オプションを使うと、curl がよしなに MIME type を multipart/form-data に設定してくれて、key=value 形式で POST することができます。
...が、@ (アットマーク) から始まる文字列は必ずファイルとして認識されてしまいます... orz

$ curl -F key=@val http://atsign.stringde.post.shitai.co.jp
curl: (26) couldn't open file "val"

$ curl -F "key=@val" http://atsign.stringde.post.shitai.co.jp
curl: (26) couldn't open file "val"

$ curl -F 'key=@val' http://atsign.stringde.post.shitai.co.jp
curl: (26) couldn't open file "val"

上の例は抽象的にしましたが、こういう風に例えば単価情報を送りたい。というケースもあるかもしれません。(@ が単価を表すとかの まとめはこちら。)

$ curl -F "unit_price=@100" http://atsign.stringde.post.shitai.co.jp

--form-string オプションを使えばできる

curl -d オプションで、application/x-www-form-urlencoded 形式で POST するしかないかな〜と思っていましたが、--form-string なるオプションを curl は用意してくれていました。

To send a field value literally without interpreting a leading '@'
or '<', or an embedded ';type=', use --form-string instead of
-F. This is recommended when the value is obtained from a user or
some other unpredictable source. Under these circumstances, using
-F instead of --form-string would allow a user to trick curl into
uploading a file.

これで解決。

$ curl --form-string "unit_price=@100" http://atsign.stringde.post.shitai.co.jp
0
1
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
0
1