Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I POST XML data with curl

Tags:

http

post

curl

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()); } 
like image 347
Jan Jongboom Avatar asked Jan 14 '10 10:01

Jan Jongboom


People also ask

How does Curl respond to XML?

To send XML to the server using Curl, you need to pass the XML data to Curl with the -d command line option and specify the data type in the body of the POST message using the -H "Content-Type: application/xml" command-line option.

Can you post with Curl?

Users can send data with the POST request using the -d flag. The following POST request sends a user and a pass field along with their corresponding values. POSTing with curl's -d option will include a default header that looks like: Content-Type: application/x-www-form-urlencoded .


1 Answers

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

-H "Content-Type: text/xml"  
like image 132
Ben James Avatar answered Oct 03 '22 00:10

Ben James