b11
b11

Reputation: 67

Properly create CURL calls using loop in bash script

I need to call an API where I increment the user ID every time, I have the following in the bash script, but keep getting a Unexpected token ' in JSON at position 2 error. What am I doing wrong?

for ((i=1;i<=5;i++)); do
    curl -X POST --header 'Content-Type: application/json' -d "{ 'id': 'person'$i, 'name': 
    'person', 'info': {} }" 'http://localhost:9999/add'

Upvotes: 1

Views: 3019

Answers (2)

OneMoreQuestion
OneMoreQuestion

Reputation: 1753

It is a quoting issue. It is standard for JSON to have double quotes, try this

for ((i=1;i<=5;i++)); do
  echo "Adding person"$i
  curl -X POST --header 'Content-Type: application/json' --header 
  'Accept: application/json' --user 'admin' -d '{ "id": "person'$i'", "name": 
  "person", "info": {} }" 'http://localhost:9999/add'
done

Upvotes: 2

dalmo.santos
dalmo.santos

Reputation: 79

You can use jq, to ​​edit json by shellscript. See this link.

Upvotes: 0

Related Questions