0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

cURL を使って POST を実行する

Posted at

はじめに

よく忘れるので、備忘録として残しておきます。

ちなみに、cURL を使った GET リクエストは以下でできます。

bash
curl http://localhost

通常のリクエスト (送信データなし)

bash
curl -X POST http://localhost/test

送信データを付ける場合

json 形式で送信する場合。

bash
curl -X POST -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}' http://localhost/test

リクエストにポート番号を付ける場合

ポート番号は8000とした場合。

bash
curl -X POST -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}' http://localhost:8000/test

リクエストにヘッダーを付ける場合

ポート番号は8000とした場合。

bash
curl -X POST -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}' http://localhost:8000/test

リクエストに複数ヘッダーを付ける場合

-H オプションを複数回使用。

bash
curl -X POST -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}' http://localhost/test

おわりに

cURL を使って POST 実行をする方法をまとめました。XML 形式やファイルデータを送信したい場合は、-HContent-Type: application/xmlContent-Type: image/jpeg になりますので注意しましょう。

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?