I am trying to do a curl call in my shell script where i want to pass the result of sql in data.
Please assume I have result stored in array as liveCalculators
curl_response=`curl -k -X POST "https://myapiurl" -H "accept: */*" -H "Content-Type: application/json" -d "{ \"keys\": [], \"calcs\": ${liveCalculators[@]}}" -w ";%{http_code}"`
I get Bad Request as error
Please suggest what can be done to fix it.
CodePudding user response:
Like this :
(you need to deal how to populate calcs array
before)
curl -k -X POST "https://myapiurl" \
-H "accept: */*" \
-H "Content-Type: application/json" \
-w ";%{http_code}" \
-d "@/dev/stdin" <<EOF
{ "keys": [], "calcs": [] }
EOF
Id liveCalculators
is a shell array, not sure it works as-is. Tell us
CodePudding user response:
Your array output is not encapsulated as a valid JSON, so how about this:
curl_response=$(curl -k -X POST "https://myapiurl" -H "accept: */*" -H "Content-Type: application/json" -d "{ \"keys\": [], \"calcs\": \"${liveCalculators[@]}\" }" -w ";%{http_code}")
In addition, the output of the array liveCalculators
is a BASH list, and you might need to do some parsing and search/replace, to convert it to a valid JSON list..