Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I post XML to RESTFUL Web Service using Net::HTTP::Post?

I am having trouble getting this to work so any help would be appreciated! Basically, the request.body contains valid XML for the Web Service like so:

<somedata>
<name>Test Name 1</name>
<description>Some data for Unit testing</description>
</somedata>

...but the service returns empty XML. Note that the id field is returned suggesting that it does actually hit the database, but the name and description fields are nil:

<somedata>
<id type='integer'>1</id>
<name nil='true'></name>
<description nil='true'></description>
</somedata>

I have manually tested the RESTFUL service using Poster and it works fine.

Here is the code:

url = URI.parse('http://localhost:3000/someservice/')
request = Net::HTTP::Post.new(url.path)
request.body = "<?xml version='1.0' encoding='UTF-8'?><somedata><name>Test Name 1</name><description>Some data for Unit testing</description></somedata>"
response = Net::HTTP.start(url.host, url.port) {|http| http.request(request)}

#Note this test PASSES!
assert_equal '201 Created', response.get_fields('Status')[0]

Does anyone have any clues why the data in the XML post is not persisted?

like image 204
Darren Jensen Avatar asked Mar 10 '09 10:03

Darren Jensen


People also ask

How do I send XML data to a restful web service?

If you want to send XML data to the server, set the Request Header correctly to be read by the sever as XML. xmlhttp. setRequestHeader('Content-Type', 'text/xml'); Use the send() method to send the request, along with any XML data.

Can we send XML data in REST API?

The REST API Client Service currently accepts only JSON input when making REST API Create, Read, Update, or Delete requests. It is nevertheless possible to use XML input when making REST API requests.

How do I POST XML data in Web API?

To post XML data to the server, you need to make an HTTP POST request, include the XML in the body of the request message, and set the correct MIME type for the XML. The correct MIME type for XML is application/xml.

How do I send a POST request in XML?

Send XML requests with the raw data type, then set the Content-Type to text/xml . After creating a request, use the dropdown to change the request type to POST. Open the Body tab and check the data type for raw. Click Send to submit your XML Request to the specified server.


1 Answers

Without knowing anything about the service, this is just a guess, but… is the service expecting a specific header that Poster is setting and you aren't?

like image 155
Hank Gay Avatar answered Oct 21 '22 01:10

Hank Gay