Skip to main content
6 of 6
added 1 character in body
David Todd
  • 47
  • 1
  • 1
  • 7

Modify template file on the fly and post it to server using curl?

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 
David Todd
  • 47
  • 1
  • 1
  • 7