0

I am executing curl command inside bash script.

Issue is in curl command I have to pass some variables($summary,$description). but as these variables are in single quote('), variables are not getting assigned.

FYI: I can't remove single quote and also can't replace with double quotes.

How can I overcome this situation.

Curl Command:

curl -X POST -d '{"summary": "$summary",  "description": "$description", "moduleMapAssets": [{"name":"Rates | IRD"},{"name":"CRD | CRD"}]}' -H "Content-Type: application/json"
Ankit Goyal
  • 151
  • 1
  • 12
  • "I can't remove single quote and also can't replace with double quotes." -> with that limitation, what you want cannot be done. Variables won't be interpreted inside single quotes. Why do you say you can't remove or change them? – brunorey Mar 20 '19 at 18:10
  • In curl command string after -d should be passed in single quote. I tried by removing it and also replacing it with double quote but in this case command through bad request error. so looking for workaround. – Ankit Goyal Mar 20 '19 at 18:19
  • "In curl command string after -d should be passed in single quote." No, sir. – brunorey Mar 20 '19 at 18:32
  • Use a tool like `jq` to generate JSON like this; it takes care of making sure the contents of `summary` and `description` get escaped properly when necessary. – chepner Mar 20 '19 at 18:42

2 Answers2

2

You can (and must) use double-quotes, you just need to escape the double-quotes that are part of the string:

curl -X POST -d "{\"summary\": \"$summary\",  \"description\": \"$description\", \"moduleMapAssets\": [{\"name\":\"Rates | IRD\"},{\"name\":\"CRD | CRD\"}]}" -H "Content-Type: application/json"

As @MikeHolt pointed out in a comment, it's also possible to mix quoting styles within a single string, so you could switch back and forth between single-quoted sections that include literal double-quotes, and double-quoted sections that include variable references:

curl -X POST -d '{"summary": "'"$summary"'",  "description": "'"$description"'", "moduleMapAssets": [{"name":"Rates | IRD"},{"name":"CRD | CRD"}]}' -H "Content-Type: application/json"

To explain that in a little more detail: ... '{"summary": "'"$summary"'", "description"...' is parsed as the single-quoted section '{"summary": "' (within which the double-quotes are literal), followed immediately by the double-quoted section "$summary" (within which the variable gets expanded), followed immediately by another single-quoted section '", "description"...' etc. Since there are no spaces between these sections, they're treated as one long argument to curl.

BTW, if any of your variables can contain double-quotes or backslashes themselves, things get much more complicated. If something like this is a possibility, you should be using something like jq to create the string. Something like this:

jsonstring=$(jq -n --arg summary "$summary" --arg description "$description" '{
    summary: $summary,
    description: $description,
    moduleMapAssets: [{name: "Rates | IRD"}, {name: "CRD | CRD"}]
    }' )
curl -X POST -d "$jsonstring" -H "Content-Type: application/json"
Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
  • It's also worth noting that bash will happily concatenate strings with different quoting styles, as long as you don't have any whitespace in between, e.g., `'foo'"bar"` is equivalent to `"foobar"`. It might ease the burden of having to escape every single `"`. – Mike Holt Mar 20 '19 at 18:19
  • @MikeHolt Good point; see update. – Gordon Davisson Mar 20 '19 at 18:27
-3

try use with backslash on sigle-quotes

curl -X POST -d \'{"summary": "$summary",  "description": "$description", "moduleMapAssets": [{"name":"Rates | IRD"},{"name":"CRD | CRD"}]}\' -H "Content-Type: application/json"
Esli Silva
  • 521
  • 6
  • 10