views:

179

answers:

2

I'm trying to post form data to Google Checkout using the following code:

x = Net::HTTP.post_form(URI.parse('https://checkout.google.com/api/checkout/v2/checkoutForm/Merchant/[merchant_number_here]'), @params)

When I attempt to submit using this line, I get the following error:

Errno::ECONNRESET in PaymentsController#create
Connection reset by peer

Any thoughts on what could be going wrong?

A: 

You need to do a fair bit more than that to get Net::HTTP to perform an HTTPS post. Paul Goscicki has a good summary, including sample code, in his post titled Custom HTTPS Queries in Ruby.

Personally I'd suggest looking into Mechanize. It's much quicker & easier to use, and has a bunch of nice features including the ability to follow redirects & handle cookies.

Duncan Bayne
A: 

The method post_form tries to connect over HTTP even of the uri is HTTPS. You need to explicitly tell net/http that a secure connection should be used. The script below should do what you want. You can use the set_debug_output method to debug the response returned by Google.

require 'net/http'
require 'net/https'

url = URI.parse('https://checkout.google.com/api/checkout/v2/checkoutForm/Merchant/1234567890')
req = Net::HTTP::Post.new(url.path)

req.set_form_data({'my'=>'params'})
res = Net::HTTP.new(url.host, url.port)
res.use_ssl = true

#send the response to stderr for debugging
res.set_debug_output $stderr

res.start {|http| http.request(req) }
mattfawcett