1

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'
b11
  • 67
  • 1
  • 1
  • 9

2 Answers2

1

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
OneMoreQuestion
  • 1,693
  • 3
  • 25
  • 51
0

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