TAGS :Viewed: 7 - Published at: a few seconds ago

[ cURL in Bash - Passing Variables Between Single Quotes ]

I am trying to pass parameters to cURL through the command line, this way:

curl -s -X POST -H "Content-Type: text/xml" -H "Cache-Control: no-cache" -d '<Data Token="someToken" Name='"$appName"' ID='"$someVar"' ParseAppID='"$someVar"' ParseRESTKey='"$someVar"' AndroidPackage='"$someVar"' Version="1"></Data>' 'https://prefix.something.com/somePath?InputType=Xml'

(This line is actually extracted from the Postman app).

I Googled this issue and found whole lot of solutions that did not work for me (links are to SO past questions...):

  1. I tried isolating the variables by ending the single quotes, this way: 'before...'"${someVar}"'...after...'. Could not complete the request.
  2. I tried passing the variables using a file (-d @fileName). Failed to post.
  3. I tried replacing the single quotes surrounding the <Data> tokens with double quotes - but the command apparently cannot accept such substitution.

The errors I get are either <Error></Error> or The server encountered an error and could not complete your request.

Is there any chance that there exists some other solution? Has anyone encoutered such problem before?

I would be greatful for any help.

Answer 1

You aren't supplying quotes around the value of ID like you are for Name. That is, you need

'<Data Token="someToken" Name="'"$appName"'" ...>'
                              ||+- shell quote to protect $appName
                              |+- shell quote enclosing the XML
                              +- literal quote embedded in the XML

which results in the string (assuming appName=foo)

<Data Token="someToken" Name="foo" ...>