0

I have below input temp.txt file which I need to post it using curl -

{
  "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}"
}

Below is how I am sourcing the file and posting it to server. Everything works fine without any issues.

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}'

Now I am trying to post above curl request for multiple clientId's as shown in my above temp.txt file. I have like 10 different clientId's for which I want to post the same json but for each different clientId.

Is there any way I can make this generic enough so that it can read clientId's from some other file say - clientIds.txt which will have list of all clientId's and then it can post each json for its own clientId to the server?

I can have content in clientIds.txt file as -

1234
9812
6751
2181

Now I should make json like this for each clientId and post it to server. Is this possible to do by any chance? I can have temp.txt file as the template where clientId field can be filled from clientIds.txt file and then we can post it out to the server but I am not sure how we can do this in shell script?

For clientId: 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}"
}

For clientId: 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}"
}

For clientId: 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}"
}

Update

I tried something like this in my bash script but I cannot make it work as I am confuse on few things. How can I replace my template json file for each clientId and then post that json to the server using curl -

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

0

You could create a bash file that loops through that list:

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

replace the echo curl command with your working curl command, but the $word variable is the next clientId in order

Or just ran that code-block directly in the cli.

3
  • yeah that part I got it. My only confusion is how to change temp.txt template for each clientId from clientIds.txt file. Commented Jun 3, 2021 at 1:40
  • Also updated my question with what I have tried. I am running it on my mac for now. Commented Jun 3, 2021 at 1:42
  • I don't get what you are asking. My answer described how to change the template for each word from the file in question. Commented Jun 3, 2021 at 3:21

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.