1

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.

Community
  • 1
  • 1
Ryan
  • 5,644
  • 3
  • 38
  • 66

1 Answers1

3

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.

Ryan
  • 5,644
  • 3
  • 38
  • 66