1

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.

Community
  • 1
  • 1
Ophir Harpaz
  • 27
  • 1
  • 4
  • 1
    Dropping out of single quotes is the only way to get the variables to get expanded. That said whether the contents of the variables are legal in that location is a different question. You may very well construct an illegal request that way but that's not a shell variable issue. What was your exact failure when you tried that? – Etan Reisner Jul 27 '15 at 15:24
  • @EtanReisner thanks for the comment. When I do something like I do something like `'' `, the error I get is empty: ``. I tried echoing the variables prior to this command, and they are all correctly printed. – Ophir Harpaz Jul 27 '15 at 15:32
  • Providing a way to do this where the output is guaranteed to always be valid XML, by the way, would be a different, longer answer. @chepner has the cheap/easy approach covered; ensuring document validity (entity escaping and the like) calls for different tools. – Charles Duffy Jul 27 '15 at 15:41

1 Answers1

9

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" ...>
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Hi @chepner and thanks. I tried applying this on all parameters like this: `curl -s -X POST -H "Content-Type: text/xml" -H "Cache-Control: no-cache" -d '' 'someURL'` and still got ``. Are the changes correct? – Ophir Harpaz Jul 27 '15 at 15:43
  • 2
    @OphirHarpaz, if you want to know if your changes are correct, use `bash -x yourscript` to see exactly what is being passed to curl (though that's shell-escaped, as opposed to byte-for-byte). That said, on a glance, what you're doing there looks good to me -- which is to say that it should be substituting in the variables in question; whether the result is a valid document is a different discussion. – Charles Duffy Jul 27 '15 at 15:43
  • With what Charles says, I would try a sample query where you hard-code *everything* to make sure that you are getting the query correct, independent of any shell quoting. – chepner Jul 27 '15 at 15:47
  • @CharlesDuffy okay, thanks a lot. I will try that and examine if I truly pass valid parameters. Thanks again, your answers helped! – Ophir Harpaz Jul 27 '15 at 15:48