使用bash脚本中的循环正确创建CURL调用

时间:2018-11-16 20:31:20

标签: json bash shell curl devops

我需要调用一个API,每次我都在其中递增用户ID,bash脚本中包含以下内容,但始终出现Unexpected token ' in JSON at position 2错误。我在做什么错了?

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'

2 个答案:

答案 0 :(得分:0)

您可以使用 jq 通过shellscript编辑json。参见此link

答案 1 :(得分:0)

这是一个报价问题。 JSON具有双引号是标准做法,请尝试

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