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 3 years have passed since last update.

Bourne shellスクリプト中でcurlにbodyを渡すときはヒアドキュメントを使うと見通しが良い

0
Last updated at Posted at 2022-02-07

タイトル通りの小ネタです。ほぼ個人的な備忘録です。常識だったらすみません。

Web APIの挙動を cURL を使ってサンプルで示す場合が結構あるかと思いますが、POSTメソッドなどでBodyを取る場合にどうやって表記すると見た目がわかりやすいかと迷っていましたが、ヒアドキュメントを使って書くと比較的見通しが良さそうです。

curl --request POST --header "$SOME_HEADER" -d@- $URL <<EOM
{
   "some_field": "value",
   "other_field": "$OTHER_FIELD"
}
EOM

ポイント

  • -d@-: このオプションを付けると標準入力からBODYを取得する。
  • ヒアドキュメントの形式で書くと:
    • 変数展開が可能。
    • エスケープをあまりしなくてすむ。
0
0
2

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?