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 = Net::HTTP.new('server.com')
http.send_request('GET', path, parameters, headers)
try:
http = Net::HTTP.new('server.com')
http.send_request('GET', path + '?' + parameters, '', headers)
- The red metacoder side of a mad betacoder scientist -
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:
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)
I found that by seeing code at http://www.rubyinside.com/nethttp-cheat-sheet-2940.html.
http.use_ssl = true
response = http.send_request('GET', path, parameters, headers)
I found that by seeing code at http://www.rubyinside.com/nethttp-cheat-sheet-2940.html.
Subscribe to:
Posts (Atom)