I can't say enough good things about Nick Sieger's multipart-post library.
It adds support for multipart posting directly to Net::HTTP, removing your need to manually worry about boundaries or big libraries that may have different goals than your own.
Here is a little example on how to use it from the README:
url = URI.parse('http://www.example.com/upload')
File.open("./image.jpg") do |jpg|
req = Net::HTTP::Post::Multipart.new url.path,
"file" => UploadIO.new(jpg, "image/jpeg", "image.jpg")
res = Net::HTTP.start(url.host, url.port) do |http|
$ sudo gem install multipart-post
If you're connecting via SSL you need to start the connection like this:
n = Net::HTTP.new(url.host, url.port)
n.use_ssl = true
# for debugging dev server
#n.verify_mode = OpenSSL::SSL::VERIFY_NONE
res = n.start do |http|
That one did it for me, exactly what I was looking for and exactly what should be included without the need for a gem. Ruby is so far ahead, yet so far behind.
awesome, this comes as a God send! used this to monkeypatch the OAuth gem to support file uploads. took me only 5 minutes.
@matthias I'm trying to upload photo with OAuth gem, but failed. could you give me some example of your monkeypatch?
The patch was quite specific to my script (quick-and-dirty), but have a look at it and maybe you can some up with a more generic approach (gist.github.com/974084)
Multipart does not support request headers. So if you for example want to use the JIRA REST interface, multipart will just be a waste of valuable time.