Neethu Lalitha
Neethu Lalitha

Reputation: 3071

POSTing XML content to curl through a variable

I am trying to do a curl post command in my shell script. I want to POST an xml , which is in a variable $xml_content .

I tried using below commands:

 curl --request POST   --url "$URL"   --header 'cache-control: no-cache'   --header 'content-type: application/xml' --data  ${xml_content}


echo ${xml_content} | xmllint format -| curl --request POST   --url "$URL"   --header 'cache-control: no-cache'   --header 'content-type: application/xml' --data @-

Both of them shows below response :

Request is an invalid format Invalid Request

But when I use:

curl --request POST   --url "$URL"   --header 'cache-control: no-cache'   --header 'content-type: application/xml' --data  "the content to be posted ie the xml content itself without using variable name"

it works.

Question : How should a variable be passed into a curl post . Am i using correct syntax?

Upvotes: 2

Views: 3081

Answers (1)

codeforester
codeforester

Reputation: 42999

The issue here is not curl, but the way shell passes the arguments to curl.

As xml_content might contain characters that have special significance to shell, you need to put it in double quotes while passing to curl:

curl --request POST --url "$URL" --header 'cache-control: no-cache' --header 'content-type: application/xml' --data "$xml_content"

Take a look at this post on Unix & Linux Stack Exchange which talks about the topic of quoting:

https://unix.stackexchange.com/questions/68694/when-is-double-quoting-necessary

Upvotes: 2

Related Questions