Saturday, October 27, 2018

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 = Net::HTTP.new('server.com', 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 = Net::HTTP.new('server.com', 443)
      http.use_ssl = true
      response = http.send_request('GET', path, parameters, headers)


No comments:

Post a Comment