rubywebrick

How to receive a multipart POST using WEBrick, ideally streaming (not in memory)


I wish to receive a multipart POST of large files. I have not been able to find any real support for this using WEBrick. All I have found is if you use request.query(), you get a HASH of the data indexed using the names from the headers. However, ruby hashes are not ordered, so this does not allow you to reconstruct the data correctly. Also, it would be nice to stream the data to a file, instead of loading it entirely in memory.


Solution

  • So I figured it out except for the streaming. As long all the headers for the multipart chunks have the same name (Content-Disposition: form-data; name=\"some_name\"), all the data will be grouped together in a WEBrick::HTTPUtils::FormData object, accessed using:

    request.query['some_name']
    

    and each data element can be iterated over in order using:

    request.query['some_name'].each_data {|data| puts data.to_s}