Net::HTTPResponse.body_permitted?() Issue

Hi,

I am playing arround with Net::HTTP and have this little script to
grap a websites HTML:

require “net/http”
page = “http://www.google.co.uk
response = Net::HTTP.get_response(URI.parse(page))
puts response.body if response.body_permitted?

I aim to print out the body of the response object only if it is
permitted but I get this:

NoMethodError: undefined method `body_permitted?’ for #<Net::HTTPOK
200 OK readbody=true>

So I am not sure what is going on as get_response returns an
HTTPResponce object so calling body_permitted?’ on it should work.

Any ideas?

David.

On Sat, Mar 10, 2007 at 09:25:57PM +0900, David M. wrote:

NoMethodError: undefined method `body_permitted?’ for #<Net::HTTPOK
200 OK readbody=true>

So I am not sure what is going on as get_response returns an
HTTPResponce object so calling body_permitted?’ on it should work.

If you mean you want to print the body only for a 200 (OK) response,
then
one way is

require “net/http”
page = “http://www.google.co.uk
response = Net::HTTP.get_response(URI.parse(page))
puts response.body if Net::HTTPSuccess === response

(“ri Net::HTTP” gives a bunch of examples; if you search for
get_response
you find this one, except written as a case statement)

I did a search for body_permitted? in /usr/lib/ruby/1.8/net/http.rb, and
I
see it’s defined as a class method, not an instance method. So the
following
also works:

require “net/http”
page = “http://www.google.co.uk
response = Net::HTTP.get_response(URI.parse(page))
puts response.body if response.class.body_permitted?

B.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs