I would like to loop on an array of strings defined inside a bash script (no external feed of the input parameters from a file). The cURL command is working fine when outside the loop passing a string variable to it (see the following FIRST SCENARIO), however when I try to do something similar on a loop I fail to get the HTTP response (see the following SECOND SCENARIO), I get instead:
./curlScript.sh: line N: "paramOne" : [" foo bar"]: command not found
./curlScript.sh: line N+1: "paramOne" : "foo bar": command not found
./curlScript.sh: line N+2: "paramOne" : [{"detailOne":100.0, "detailTwo":"foo bar"}]: command not found
The idea is to pass JSON object in a HTTP POST request.
I've tried different quoting attempts: ', or ', or no quoting in different combinations around the variable $JSON_PARAM_V2, no way to get this working.
The curlScript.sh is executable (chmod +x).
What's wrong with the following script?
#!/bin/bash
IP_URL="localhost"
JSON_PARAM="foo bar"
# FIRST SCENARIO
curl -X POST -d '{ "paramOne" : "'"$JSON_PARAM"'" }' http://$IP_URL:8080/url_path --header "Content-Type:application/json"
sleep 2;
# SECOND SCENARIO
jsonParametersArray=
(
"\"paramOne\":[\"foo bar\"]"
"\"paramOne\":\"foo bar\""
"\"paramOne\":[{\"detailOne\":100.0,\"detailTwo\":\"foo bar\"}]\""
)
#for i in "${jsonParametersArray[@]}" do:
for ((i=0; i < ${#jsonParametersArray}; i++)) do
JSON_PARAM_V2=i
curl -X POST -d '{ "paramOne" : '"$JSON_PARAM_V2"' }' http://$IP_URL:8080/url_path --header "Content-Type:application/json"
sleep 2;
done