http icon indicating copy to clipboard operation
http copied to clipboard

Can't use IO.copy_stream

Open davispuh opened this issue 6 years ago • 3 comments

Basically I want to stream HTTP body to a file.

I tried to do this but sadly it doesn't work

File::open('file.dat','w') do |file|
    body = HTTP.get(uri).body
    IO.copy_stream(body, file)
end
/usr/lib/ruby/gems/2.6.0/gems/http-4.1.1/lib/http/response/body.rb:30:in `readpartial'
/usr/lib/ruby/gems/2.6.0/gems/http-4.1.1/lib/http/connection.rb:86:in `readpartial': wrong number of arguments (given 2, expected 0..1) (ArgumentError)

It happens because HTTP::Connection.readpartial doesn't accept 2nd argument of buffer which is expected by IO.copy_stream

davispuh avatar Oct 04 '19 18:10 davispuh

After some mucking around in the debugger I was able to sort of work around this with

streamer = body.instance_variable_get(:@stream)
io = streamer.instance_variable_get(:@io)

A bit awful though, and I'm not sure it's going to work reliably. Isn't there some way to just get the body as an IO?


Update: Nope, totally not reliable.

dmolesUC avatar Feb 25 '21 01:02 dmolesUC

The best I've been able to come up with is to wrap body.string in a StringIO -- which obviously isn't ideal for large bodies. I tried using IO.pipe to copy chunks to an IO, but ran into a weird encoding issue when I tried to make it work under Rails.

dmolesUC avatar Feb 25 '21 20:02 dmolesUC

just encountered this issue, can't believe it

andrewchen5678 avatar Nov 20 '21 03:11 andrewchen5678