Debugging a failed request
In the previous example you saw that I had to set :use_ssl => true
, but it took some time to figure it out.
The main problem was that in case of failure my previous code only printed the HTTP status code that was 400.
I read the documentation several times and tried various combinations of the code till it occurred to be that maybe
there is some hint in the body
of the response. So I changed the code to print that in case of failure.
require 'net/http'
url = 'https://httpbin.org/post'
uri = URI(url)
req = Net::HTTP::Post.new(uri)
req.set_form_data(
"name" => "Foo Bar",
"height" => 175,
)
req['User-Agent'] = "Internet Explorer 6.0"
response = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
if response.is_a?(Net::HTTPSuccess)
puts response.body
else
#puts response.code
puts response.body
end
This was the response:
<html>
<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<center>The plain HTTP request was sent to HTTPS port</center>
</body>
</html>
That allowed me to understand that the issue is around the use of ssl: http vs https. First I tried the same code replacing the URL by one that uses http:
url = 'http://httpbin.org/post'
This worked.
At this point I understood I need to search for something about ssl, and that's how I found out that I had to pass :use_ssl => true
.
So remember, at least for debugging, it can be useful to print the content of the body even in the case of error.