简体   繁体   English

Rails 3发布到外部Web服务

[英]Rails 3 Post to external web service

Lets say I have a blog post that a user is creating and I want to send all of the data to an external web service as XML with a specific schema so it can be ingested into that web service. 可以说,我有一个用户正在创建的博客文章,我想将所有数据作为具有特定模式的XML发送到外部Web服务,以便可以将其提取到该Web服务中。

I have been looking into the ActionDispatch::Request 我一直在研究ActionDispatch :: Request

And I read this Using Ruby on Rails to POST JSON/XML data to a web service post and answer 我读了《 使用Ruby on Rails将JSON / XML数据发布到Web服务》并回答

However I got an error saying content_type was not a valid method for request. 但是我收到一个错误消息,说content_type不是有效的请求方法。 So I changed that line to call the header method and create a header for content-type with the appropriate information 因此,我更改了该行以调用header方法,并使用适当的信息为content-type创建一个header

Ok... so now where to go? 好吧...那现在去哪里?

This is my code so far: 到目前为止,这是我的代码:

url= URI.parse('http://10.29.3.47:8080/ingest')
response = Net::HTTP::Post.new(url.path)
request.headers["Content-Type"] = 'application/json'
request.body = 'all of my xml data and schema which is far too long to type here'
response = Net::HTTP.start(url.host, url.port) {|http| http.request(request)}
assert_equal '201 Created', response.get_fields('Status')

I get an error saying that request.body is also not a valid method call, but when I look at the API the only thing matching body is "body()" which does not take arguments. 我收到一个错误消息,说request.body也不是有效的方法调用,但是当我查看API时,唯一匹配的主体是不带参数的“ body()”。 So how do I pass the content of my post to the web service? 那么如何将帖子的内容传递到Web服务?

Thank you for the help! 感谢您的帮助!

You had response = Net::HTTP::Post.new(url.path) instead of request = Net::HTTP::Post.new(url.path) and you add headers with add_field . 您有一个response = Net::HTTP::Post.new(url.path)而不是request = Net::HTTP::Post.new(url.path)并使用add_field 添加标头。

require 'net/http'
require 'uri'
url= URI.parse('http://10.29.3.47:8080/ingest')
request = Net::HTTP::Post.new(url.path)
request.add_field 'Content-Type', 'application/json'
request.body = 'all of my xml data and schema which is far too long to type here'
response = Net::HTTP.start(url.host, url.port) {|http| http.request(request)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM