# How to get an IO object for the response from an HTTP get?

**URL:** <https://www.ruby-forum.com/t/how-to-get-an-io-object-for-the-response-from-an-http-get/100074>\
**Category:** Ruby\
**Created:** [July 27, 2006, 12:29am UTC](https://www.ruby-forum.com/t/how-to-get-an-io-object-for-the-response-from-an-http-get/100074 "2006-07-27T00:29:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tim\_o](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/9d8465/32.png) [@tim\_o](https://www.ruby-forum.com/u/tim_o)\
**Post date:** [July 27, 2006, 12:29am UTC](https://www.ruby-forum.com/t/how-to-get-an-io-object-for-the-response-from-an-http-get/100074/1 "2006-07-27T00:29:26Z")

</div>

Hi,

I’d like to get an IO object instead of a string for the response of  
an HTTP get. I am using the Net/HTTP library but it seems to only  
want to give me a string. I have a situation where the response can  
be quite large and do not want the whole response to have to be in  
memory.

Is it possible to get an IO object for the response without putting  
the whole response in memory?

thanks,  
Tim

---

<div class="post-metadata">

**Author:** ![tim\_o](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/9d8465/32.png) [@tim\_o](https://www.ruby-forum.com/u/tim_o)\
**Post date:** [July 27, 2006, 5:08am UTC](https://www.ruby-forum.com/t/how-to-get-an-io-object-for-the-response-from-an-http-get/100074/2 "2006-07-27T05:08:31Z")

</div>

On 7/27/06, Tim O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I’d like to get an IO object instead of a string for the response of  
> an HTTP get. I am using the Net/HTTP library but it seems to only  
> want to give me a string. I have a situation where the response can  
> be quite large and do not want the whole response to have to be in  
> memory.
> 
> Is it possible to get an IO object for the response without putting  
> the whole response in memory?

It’s possible to pass a block to HTTPResponse#get, which will yield a  
chunk of data each time. That said, you’re probably better served by  
using open-uri, which lets you do this:

require ‘open-uri’  
open(‘[http://www.google.com](http://www.google.com)’) do |f|  
#f is an IO object, yay!  
end

Sam

---

<div class="post-metadata">

**Author:** ![tim\_o](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/9d8465/32.png) [@tim\_o](https://www.ruby-forum.com/u/tim_o)\
**Post date:** [July 27, 2006, 5:45pm UTC](https://www.ruby-forum.com/t/how-to-get-an-io-object-for-the-response-from-an-http-get/100074/3 "2006-07-27T17:45:29Z")

</div>

On 7/26/06, Sam G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It’s possible to pass a block to HTTPResponse#get, which will yield a  
> chunk of data each time. That said, you’re probably better served by  
> using open-uri, which lets you do this:

irb(main):001:0\> require ‘net/http’  
=\> true  
irb(main):002:0\> Net::HTTPResponse.get  
NoMethodError: undefined method `get’ for Net::HTTPResponse:Class  
from (irb):2  
irb(main):003:0\>

I’d rather use the HTTP library because I’d like to use this for other  
HTTP methods as well, such as propget, etc.

thanks,  
Tim

---

<div class="post-metadata">

**Author:** ![tim\_o](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/9d8465/32.png) [@tim\_o](https://www.ruby-forum.com/u/tim_o)\
**Post date:** [July 27, 2006, 6:14pm UTC](https://www.ruby-forum.com/t/how-to-get-an-io-object-for-the-response-from-an-http-get/100074/4 "2006-07-27T18:14:34Z")

</div>

I think I figured it out. I can use HTTPResponse#read\_body

thanks for the help!

-Tim
