如何使用Net :: HTTP :: Post将XML发布到RESTFUL Web服务? [英] How do I post XML to RESTFUL Web Service using Net::HTTP::Post?
问题描述
我无法使它正常工作,因此我们将不胜感激!基本上,request.body包含Web服务的有效XML,如下所示:
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>
...但是该服务返回空XML.请注意,返回的id字段表明它确实确实命中了数据库,但是name和description字段为nil:
...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>
我已经使用Poster手动测试了RESTFUL服务,并且效果很好.
I have manually tested the RESTFUL service using Poster and it works fine.
这是代码:
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]
有人知道为什么XML帖子中的数据不能持久保存的任何线索吗?
Does anyone have any clues why the data in the XML post is not persisted?
推荐答案
在不了解有关服务的任何情况下,这只是一个猜测,但是……服务是否期望Poster设置了特定的标头,而您却没有?
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?
这篇关于如何使用Net :: HTTP :: Post将XML发布到RESTFUL Web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!