LoginSignup
246

More than 5 years have passed since last update.

curlコマンドから HTTP POST する方法

Last updated at Posted at 2014-03-12

普通のname/valueペアをPOSTする

curl -F "name1=value1" -F "name2=value2" http://yourdomain/execute.script

これが

<form method="POST" action="http://yourdomain/execute.script">
<input type="hidden" name="name1" value="value1">
<input type="hidden" name="name2" value="value2">
<input type="submit" value="submit">
</form>

みたいな感じになります。

普通のname/valueペアに加えてファイルをPOSTする

curl -F "name1=value1" -F "name2=value2" -F "profile_icon=@path/to/file.png" -F "zip_file=@path/to/zipfile.zip" http://yourdomain/execute.script

これが

<form method="POST" action="http://yourdomain/execute.script" enctype="multipart/form-data">
<input type="hidden" name="name1" value="value1">
<input type="hidden" name="name2" value="value2">
<input type="file" name="profile_icon">
<input type="file" name="zip_file">
<input type="submit" value="submit">
</form>

みたいな感じになります。

みたいな感じとは?

multipart/form-dataなど、フォーム送信する際の方式がいくつかあり、厳密に同じか確認していません。
しかも、データを受け取って保存するところまで確認していません!がきっと大丈夫。と思いたい。

TIPS

ファイル送信時、受け取り側のphpでvar_dump($_FILES);のみを出力し、

curl -F "name1=value1" -F "name2=value2" http://yourdomain/execute.script > path/to/public/web/directory/result.html

などと指定しておくと、curlの結果がわかりやすかったです。

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
246