Cody Brimhall
Cody Brimhall

Reputation: 1175

Buffered Multipart Form Posts in Ruby

I am currently using Net::HTTP in a Ruby script to post files to a website via a multipart form post. It works great for small files, but I frequently have to send very large files using this script, and HTTP#post only seems to accept post data as a String object, which means that the file I'm sending has to be read into memory before anything can be sent. This script is running on a busy production server, so it's unacceptable to gobble up hundreds of megabytes of RAM just to send a file.

Ideally, there'd be a method that could be given a buffer size and an IO object, and would send off buffer-sized chunks of data, reading from the IO object only as required. What would be the best way to make this happen? Did I miss something relevant in Net::HTTP?

Update: Net::HTTP#body_stream(input) looks good, though the documentation is rather... sparse. Anyone able to point me to a good example of this in action?

Upvotes: 1

Views: 1643

Answers (2)

Actually I managed to upload a file using body_stream. The full source code is here: http://stanislavvitvitskiy.blogspot.com/2008/12/multipart-post-in-ruby.html

Upvotes: 3

Vitalie
Vitalie

Reputation: 736

Use Net::HTTP#body_stream(input)

Example for multipart post without streaming:

Upvotes: 0

Related Questions