getting bad request from ruby on rails ssl post

不问归期 提交于 2019-12-13 00:23:21

问题


Looked at: Escaping parameters in set_form_data POST, and Ruby on Rails HTTPS Post Bad Request

  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

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?

-- 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.

Edit: 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

Which returns (even if I restart my server) that I have an open HTTP session.


回答1:


So ended up with this:

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)

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.



来源:https://stackoverflow.com/questions/12844277/getting-bad-request-from-ruby-on-rails-ssl-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!