http
http copied to clipboard
Can't use IO.copy_stream
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
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.
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.
just encountered this issue, can't believe it