Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cURL in Bash - Passing Variables Between Single Quotes

Tags:

bash

shell

curl

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.

like image 208
Ophir Harpaz Avatar asked Oct 20 '25 01:10

Ophir Harpaz


1 Answers

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" ...>
like image 199
chepner Avatar answered Oct 21 '25 22:10

chepner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!