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'
1
  • learn to debug your problems with simple echo curl -X ...... localhost:9999/add" Good luck. Commented Nov 16, 2018 at 23:23

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.