# Net::HTTPResponse.body\_permitted?() Issue

**URL:** <https://www.ruby-forum.com/t/net-httpresponse-body-permitted-issue/88391>\
**Category:** Ruby\
**Created:** [March 10, 2007, 1:26pm UTC](https://www.ruby-forum.com/t/net-httpresponse-body-permitted-issue/88391 "2007-03-10T13:26:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [March 10, 2007, 1:26pm UTC](https://www.ruby-forum.com/t/net-httpresponse-body-permitted-issue/88391/1 "2007-03-10T13:26:35Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [March 10, 2007, 4:13pm UTC](https://www.ruby-forum.com/t/net-httpresponse-body-permitted-issue/88391/2 "2007-03-10T16:13:00Z")

</div>

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](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](http://www.google.co.uk)”  
response = Net::HTTP.get\_response(URI.parse(page))  
puts response.body if response.class.body\_permitted?

B.
