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());
}
Community
  • 1
  • 1
Jan Jongboom
  • 26,598
  • 9
  • 83
  • 120
  • 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? – Dominic Rodger Jan 14 '10 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. – Jan Jongboom Jan 14 '10 at 10:52
  • What HTTP response code do you receive? 500? – Ben James Jan 14 '10 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... – rogerdpack Apr 04 '19 at 17:38

5 Answers5

71

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

-H "Content-Type: text/xml" 
Ben James
  • 121,135
  • 26
  • 193
  • 155
27

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 
Community
  • 1
  • 1
stones333
  • 8,588
  • 1
  • 25
  • 27
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.

Community
  • 1
  • 1
dk1844
  • 265
  • 2
  • 12
  • 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.` – Joe Atzberger Sep 28 '17 at 21:43
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/
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"
fewlinesofcode
  • 3,007
  • 1
  • 13
  • 30