1
1

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.

bashスクリプトでJSONを組み立ててcurlでPOSTする

Last updated at Posted at 2021-03-03

やりたいこと

bashスクリプトで簡単なテストデータをループで作成してcurlでPOSTしたい。

##ハマる

最初は変数にJSONをつっこんでcurlの引数に付けてましたが、ダブルクォートや波括弧が邪魔して思うように行かないのでググる。
stackoverflowで解決方法を見つけました。

ポイントはjson作成を関数にして標準出力経由でコマンドに渡す感じみたいです。
なるほど。

#!/bin/bash
set -Ceu

generate_post_data() {
    cat << EOS
{
    "Id": ${1},
    "Name": "Name${1}"
}
EOS
}

URL='http://127.0.0.1:3000/hoge'

for i in {1..10} ; do
    /usr/bin/curl --request POST --data "$(generate_post_data ${i})" "${URL}"
done

参考

1
1
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?