Net::HTTPResponse.body_permitted?() Issue

D

David Madden

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.
 
B

Brian Candler

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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top