在rails ssl post上从ruby获得错误请求 [英] getting bad request from ruby on rails ssl post
问题描述
注意:在set_form_data POST中转义参数,以及< a href =https://stackoverflow.com/questions/1719809/ruby-on-rails-https-post-bad-request> Ruby on Rails HTTPS发布错误请求
def send_request(params)
host = "https://hc.mercurydev.net"
uri = URI.parse(host)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
xml = build_xml_for_checkout(params)
http.start do |http|
req = Net::HTTP::Post.new('http://www.mercurypay.com/InitializePayment')
header = {'Host' => 'hc.mercurydev.net', 'Content-Type' => 'text/xml; charset=utf-8', 'Content-Length' => 'length', 'SOAPAction' => 'http://www.mercurypay.com/InitializePayment'}
req.set_form_data(header, xml)
response = http.request(req)
end
end
这是我第一次在ruby中构建自己的帖子请求,我是确定我错过了一些简单的东西,但是什么?
This is the first time I have had to build my own post request in ruby, I am sure that I am missing something simple, but what?
- 我确认xml是100%好的,所以它必须是我的标题或者我是如何构建请求的。
-- I have confirmation that the xml is 100% good, so it has to be something in my header or in how I'm building the request.
编辑:查看 ruby-api-doc :我想出了这个:
After looking at ruby-api-doc: I came up with this:
uri = URI.parse(host)
Net::HTTP.start(uri.hostname, uri.port,
:use_ssl => uri.scheme == 'https').start do |http|
req = Net::HTTP::Post.new uri.path
req.body = xml
req.set_form_data(header)
res = http.request req
end
返回(即使我重启服务器)我有一个开放的HTTP会话。
Which returns (even if I restart my server) that I have an open HTTP session.
推荐答案
最终结果如下:
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
req = Net::HTTP::Post.new(uri.request_uri)
req.body = xml.to_s
req.initialize_http_header(header)
response = http.request(req)
我错过的关键是 initialize_http_header()
一旦我在那里,soap api接受了我的提交,然后只是确保我的语法是正确的问题。
The key that I was missing was the initialize_http_header()
once I got that in there the soap api accepted my submission and then is was just an issue of making sure my syntax was correct.
这篇关于在rails ssl post上从ruby获得错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!