HTTParty memory issues and downloading large files

This will cause Ruby memory problems. I know that Open-URI writes to TempFile if the size exceeds 10KB. But will HTTParty try to save the entire PDF to memory before it writes to TempFile?

src = Tempfile.new("file.pdf")
src.binmode
src.write HTTParty.get("large_file.pdf").parsed_response
+5
source share
1 answer

You can use Net :: HTTP. See the documentation (in particular, the Streaming Response Devices section).

Here is an example from the docs:

uri = URI('http://example.com/large_file')

Net::HTTP.start(uri.host, uri.port) do |http|
  request = Net::HTTP::Get.new uri.request_uri

  http.request request do |response|
    open 'large_file', 'w' do |io|
      response.read_body do |chunk|
        io.write chunk
      end
    end
  end
end
+11
source

All Articles