how to get an IO object for the response from an HTTP get?

T

Tim Olsen

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
 
S

Sam Gentle

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') do |f|
#f is an IO object, yay!
end

Sam
 
T

Tim Olsen

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
 
T

Tim Olsen

I think I figured it out. I can use HTTPResponse#read_body

thanks for the help!

-Tim
 

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

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top