使用json文件中的数据分离“ curl POST”- Windows CMD shell

lhcgjxsq  于 2022-12-24  发布在  Windows
关注(0)|答案(1)|浏览(141)

我正在处理一个API连接,正在接收包含多个对象的JSON数据集,并尝试将其传递到下一个系统。
JSON数据集包含以下信息(OUTPUT.JSON):

[
  {
    "initials": "V.",
    "firstName": "Victor",
    "lastNamePrefix": " ",
    "lastName": "Rutherford",
    "employerReferenceId": "0258741",
    "jobDescription": "Rental"
  },
  {
   "initials": "P.",
    "firstName": "Pippa",
    "lastNamePrefix": " ",
    "lastName": "Lewis",
    "employerReferenceId": "98765431",
    "jobDescription": "Rental"
  },
  {
    "initials": "S.",
    "firstName": "Stephanie",
    "lastNamePrefix": " ",
    "lastName": "Reid",
    "employerReferenceId": "123456789",
    "jobDescription": "Rental"
  }
]

我尝试做的API帖子必须是JSON数据文件中每个对象(人)的单独帖子,这是因为帖子必须在URL中具有EMPLOYERREFEREBCEID的引用号,但是帖子也只需要在JSON文件中具有属于该EMPLOYERREFEREBCEID的数据部分。

curl -X POST "https://website.eu/test-Company/api2/external-employees/[EMPLOYERREFEREBCEID]" -H "accept: application/json" -H "Authorization: Token {TOKEN}" -H "Content-Type: application/json" -d "@output.json"

我确实找到了一个与JQ和Bash一起工作的旧帖子,但由于我运行的是Windows Server 2016,这将是一个挑战

for (( i = 0 ; i < ${#id[@]} ; i++ ))
do 
     POST REST API 
done

https://unix.stackexchange.com/questions/271307/post-json-data-with-curl-from-a-while-loop-bash-shell

oyxsuwqo

oyxsuwqo1#

这花了我很长时间,但我有第一部分的工作。
解决了!

for i in $(cat "c:\Test\output.json" | jq -r .[].employerReferenceId); do

在这一部分是开始 curl 命令,它也工作正常,直到数据部分,这就是我现在卡住了。

curl -X POST "https://website.eu/test-Company/api2/external-employees/$i" -H "accept: application/json" -H "Authorization: Token {TOKEN}" -H "Content-Type: application/json" -d "EXAMPLE: $initials:value $firstName:value $lastNamePrefix:value $lastName:value $employerReferenceId:value $jobDescription:value"

问题:如何引用curl命令数据部分$i中属于person的相应值。

相关问题