I am trying to deal with the http response. I am using following code to
do so.
<code>
require 'net/http'
require 'uri'
def fetch(uri_str, limit = 10)
# You should choose better exception.
raise ArgumentError, 'HTTP redirect too deep' if limit == 0
response = Net::HTTP.get_response(URI.parse(uri_str))
case response
when Net::HTTPSuccess then response
when Net::HTTPRedirection then fetch(response['location'], limit -
1)
else
response.error!
end
end
I am trying to deal with the HTTP response. I want to retrieve the
contents such as header, post, response, HTML, JSON, etc. of the HTTP
response.
(Please refer screen shot for the same.)
===
The HTTP response status code 302 Found is the most common way of
performing a redirection.[citation needed]
It is an example of industry practice contradicting the standard
HTTP/1.0 specification (RFC 1945), which required the client to perform
a temporary redirect (the original describing phrase was "Moved
Temporarily"), but popular browsers implemented it as a 303 See
Other[citation needed], i.e. changing the request type to GET regardless
of what it had been originally. Therefore, HTTP/1.1 added status codes
303 and 307 to disambiguate between the two behaviours. However, the
majority of Web applications and frameworks still use the 302 status
code as if it were the 303.[citation needed]
Did you read any of the documentation? Net::HTTP.get_response returns a
response object, because an HTTP request returns several different kinds of
things. What you are seeing there is the string representation of an
object.
Are you expecting to print the body of the web page? If so, then you
should print response.body, not response.
···
Amit Bobade <amit.srpce@gmail.com> wrote:
I am trying to deal with the http response. I am using following code to
do so.
<code>
require 'net/http'
require 'uri'
def fetch(uri_str, limit = 10)
# You should choose better exception.
raise ArgumentError, 'HTTP redirect too deep' if limit == 0
response = Net::HTTP.get_response(URI.parse(uri_str))
case response
when Net::HTTPSuccess then response
when Net::HTTPRedirection then fetch(response['location'], limit -
1)
else
response.error!
end
end