Ruby: How to post a file via HTTP as multipart/form-data


I want to do an HTTP POST that looks like an HMTL form posted from a browser. Specifically, post some text fields and a file field.

Posting text fields is straightforward, there's an example right there in the net/http rdocs, but I can't figure out how to post a file along with it.

Net::HTTP doesn't look like the best idea. curb is looking good.

Best Solution

I like RestClient. It encapsulates net/http with cool features like multipart form data:

require 'rest_client''http://localhost:3000/foo', 
  :name_of_file_param =>'/path/to/file'))

It also supports streaming.

gem install rest-client will get you started.