51

I want to post XML data with cURL. I don't care about forms like said in How do I make a post request with curl.

I want to post XML content to some webservice using cURL command line interface. Something like:

curl -H "text/xml" -d "<XmlContainer xmlns='sads'..." http://myapiurl.com/service.svc/

The above sample however cannot be processed by the service.


Reference example in C#:

WebRequest req = HttpWebRequest.Create("http://myapiurl.com/service.svc/");
req.Method = "POST";
req.ContentType = "text/xml";
using(Stream s = req.GetRequestStream())
{
    using (StreamWriter sw = new StreamWriter(s))
        sw.Write(myXMLcontent);
}
using (Stream s = req.GetResponse().GetResponseStream())
{
    using (StreamReader sr = new StreamReader(s))
        MessageBox.Show(sr.ReadToEnd());
}
4
  • What do you mean "it can't be processed by the service"? Is the service receiving it correctly? Is it being posted correctly? What does the service receive from your request? Commented Jan 14, 2010 at 10:50
  • The service doesn't recognize the request. I receive an internal error page. When using my C# example this doesn't happen. The posted data is the same. Commented Jan 14, 2010 at 10:52
  • What HTTP response code do you receive? 500? Commented Jan 14, 2010 at 11:12
  • My hunch is the data comes in "escaped" to the web service (like %3C%3Fxml+version=%221.0%22+encoding%3D%22UTF-8%) that's what I ran into in java anyway... Commented Apr 4, 2019 at 17:38

5 Answers 5

72

-H "text/xml" isn't a valid header. You need to provide the full header:

-H "Content-Type: text/xml" 
Sign up to request clarification or add additional context in comments.

Comments

29

I prefer the following command-line options:

cat req.xml | curl -X POST -H 'Content-type: text/xml' -d @- http://www.example.com

or

curl -X POST -H 'Content-type: text/xml' -d @req.xml http://www.example.com

or

curl -X POST -H 'Content-type: text/xml'  -d '<XML>data</XML>' http://www.example.com 

1 Comment

Where we can keep this @req.xml file? This is what i want to pass empty file<tsRequest> </tsRequest>
9

It is simpler to use a file (req.xml in my case) with content you want to send -- like this:

curl -H "Content-Type: text/xml" -d @req.xml -X POST http://localhost/asdf

You should consider using type 'application/xml', too (differences explained here)

Alternatively, without needing making curl actually read the file, you can use cat to spit the file into the stdout and make curl to read from stdout like this:

cat req.xml | curl -H "Content-Type: text/xml" -d @- -X POST http://localhost/asdf

Both examples should produce identical service output.

1 Comment

As a bonus, you don't need to specify -X POST with body data (because HTTP logic, obviously). If you do, modern curls will warn Note: Unnecessary use of -X or --request, POST is already inferred.
2

Have you tried url-encoding the data ? cURL can take care of that for you :

curl -H "Content-type: text/xml" --data-urlencode "<XmlContainer xmlns='sads'..." http://myapiurl.com/service.svc/

Comments

0

You can try the following solution:

curl -v -X POST -d @payload.xml https://<API Path> -k -H "Content-Type: application/xml;charset=utf-8"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.