Saturday, October 27, 2018

Net::HTTP error sending GET request with parameters

If you're getting trouble with Net::HTTP#send_request when sending a GET request with parameters, one possible cause is that parameters must be part of path as a query string, and not as data (3rd parameter of #send_request).

For example (assuming that parameters is not empty), instead of

http ='')
http.send_request('GET', path, parameters, headers)


http ='')
http.send_request('GET', path + '?' + parameters, '', headers)

Net::HTTP error 400 (bad request) with HTTPS

Hello, friends!

Yesterday I got stuck when trying to connect to an HTTPS site using Net::HTTP:

      http ='', 443)
      response = http.send_request('GET', path, parameters, headers)

I was getting error 400 in response.code.

What is not well documented is that when accessing a server via HTTPS protocol it's not enough to set port to 443. You must also set use_ssl to true:

      http ='', 443)
      http.use_ssl = true
      response = http.send_request('GET', path, parameters, headers)