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?
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.
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.
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.
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With