-3

I'm trying to update some info of a web site using curl through a script but it's not working well, this is the code i have:

CURL_STD_OPTS="-k --header 'Content-Type: application/json' --header 'Accept: application/json'
curl  "$CURL_STD_OPTS" -X POST --data '{ "actual": '"$BAL"' }' "'$websiteurl'"

and this is the output message:

jdelaoss@infcet99:/tools/saadmin/occtools_morning_checks $ ./test.sh
curl: option -k --header 'Content-Type: application/json' --header 'Accept: application/json': is unknown

if i run the script directly from the shell with the options it works.

josseossa
  • 51
  • 8

1 Answers1

4

You need to use an array, not a regular variable.

curl_std_opts=( -k --header 'Content-Type: application/json' --header 'Accept: application/json')
curl "${curl_std_opts[@]}" -X POST --data "{\"actual\": $BAL}" "$websiteurl"

For safety, you should use a tool like jq to generate your JSON rather than relying on parameter interpolation to generate valid JSON.

curl "${curl_std_opts[@]}" -X POST --data "(jq --argjson b "$BAL" '{actual: $b}') "$websiteurl"
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thanks a lot, it's working for me now. Just BTW, I had to split the arrays elements in the array as follows `CURL_STD_OPTS=("-k" "--header" "Content-Type: application/json" "--header" "Accept: application/json"` If you want to add it to the answer! – josseossa Oct 05 '18 at 15:49
  • That should produce the exact same array as in the answer; the unquoted strings don't contain any characters that require quoting, and nothing in the single-quoted strings would behave any differently inside double quotes. – chepner Oct 05 '18 at 16:05