テンプレートファイルを動的に変更し、カールを使用してサーバーに公開しますか?

テンプレートファイルを動的に変更し、カールを使用してサーバーに公開しますか?

次の入力temp.txtファイルがあり、カールを使用して公開する必要があります。

{
  "query":"\n{\n  data(clientId: 1234, filters: [{key: \"o\", value: 100}], key: \"world\") {\n    title\n    type\n    pottery {\n      text\n      pid\n      href\n      count\n      resource\n    }\n  }\n}"
}

ファイルをインポートしてサーバーに公開する方法は次のとおりです。すべてが問題なくうまく機能します。

curl 'url' \
  -H 'Accept-Encoding: gzip, deflate, br' \
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json' \
  -H 'Connection: keep-alive' -H 'DNT: 1' \
  -H 'Origin: url' \
  --data-binary "@/Users/david/Downloads/temp.txt" \
  --compressed \
  --silent \
  --output /dev/null \
  --write-out '%{http_code}'

clientId's上記のファイルに示されているように、複数のカール要求を発行したいと思いますtemp.txt。私は約10の異なるアイテムを持っておりclientId's、それぞれの異なるjsonに対して同じjsonを投稿したいと思いますclientId

たとえば、すべてのリストを含むclientId's他のファイルから読み取ることができ、各jsonをサーバーに公開できるようにこれを十分に一般化する方法はありますか?clientIds.txtclientId'sclientId

clientIds.txtファイルの内容は次のように設定できます。

1234
9812
6751
2181

これで、各ファイルに対してこれらのjsonを作成してclientIdサーバーに公開する必要があります。これは可能ですか?ファイル内のフィールドを入力し、サーバーに公開temp.txtできるテンプレートとしてファイルを持つことはできますが、シェルスクリプトでこれを行う方法がわかりません。clientIdclientIds.txt

顧客ID:9812

{
   "query":"\n{\n  data(clientId: 9812, filters: [{key: \"o\", value: 100}], key: \"world\") {\n    title\n    type\n    pottery {\n      text\n      pid\n      href\n      count\n      resource\n    }\n  }\n}"
}

顧客ID:6751

{
   "query":"\n{\n  data(clientId: 6751, filters: [{key: \"o\", value: 100}], key: \"world\") {\n    title\n    type\n    pottery {\n      text\n      pid\n      href\n      count\n      resource\n    }\n  }\n}"
}

顧客ID:2181

{
   "query":"\n{\n  data(clientId: 2181, filters: [{key: \"o\", value: 100}], key: \"world\") {\n    title\n    type\n    pottery {\n      text\n      pid\n      href\n      count\n      resource\n    }\n  }\n}"
}

修正する

Bashスクリプトで似たようなことを試しましたが、いくつかのことが混乱していて、うまくいきませんでした。各clientIdのテンプレートjs​​onファイルを置き換えてから、カールを使用してそのjsonをサーバーに公開するにはどうすればよいですか?

while IFS= read -r line; do
    # this below line isn't working
    cat temp.txt | sed 's/clientId:[^ ]*/clientId:$line/'
    # and how should I use new json to post curl
done < clientIds.txt

ベストアンサー1

リストを繰り返すbashファイルを作成できます。

#!/bin/bash
for word in $(< clientIds.txt)
do
    echo "curl $word"
done

そのコマンドをecho curl作業中のカールコマンドに置き換えます。ただし、$word変数はクライアントID~のため

またはcliから直接コードブロックを実行してください。

おすすめ記事