Class | Net::HTTP::Post |
In: |
multipart.rb
|
Parent: | Object |
Similar to Net::HTTP::Post#set_form_data (in Ruby‘s stardard library), but set up file upload parameters using the appropriate HTTP/HTML Forms multipart format.
Arguments
files_params: | A hash of file upload parameters. The keys are parameter names, and the values are Net::HTTP::FileForPost instances. See that class documentation for more info about how POST file upload works. |
other_params: | A hash of {key => value} pairs for the regular POST parameters, just like in set_form_data. Don‘t mix set_form_data and set_multipart_data; they‘ll overwrite each other‘s work. |
boundary1, boundary2: | A couple of strings which doesn‘t occur in your files. Boundary2 is only needed if you‘re using the multipart/mixed technique. The defaults should be OK for most cases. |
Examples
Simplest case (single-parameter single-file), complete:
require 'net/http' require 'rubygems' require 'multipart' req = Net::HTTP::Post.new('/scripts/upload.rb') req.basic_auth('jack', 'inflamed sense of rejection') file = Net::HTTP::FileForPost.new('/tmp/yourlife.txt', 'text/plain') req.set_multipart_data({:poem => file}, {:author => 'jack', :user_agent => 'soapfactory'}) res = Net::HTTP.new(url.host, url.port).start do |http| http.request(req) end
Convoluted example:
pic1 = Net::HTTP::FileForPost.new('pic1.jpeg', 'image/jpeg') pic2 = Net::HTTP::FileForPost.new(pic2_io, 'image/jpeg') pic3 = Net::HTTP::FileForPost.new('pic3.png', 'image/png') pic1_t = Net::HTTP::FileForPost.new('pic1_thumb.jpeg', 'image/jpeg') pic2_t = Net::HTTP::FileForPost.new(pic2_t_io, 'image/jpeg') desc = Net::HTTP::FileForPost.new('desc.html', 'text/html', 'index.html') # remote fname req.set_multipart_data({:gallery_description => des, :pictures => [pic1, pic2, pic3], :thumbnails => [pic1_t, pic2_t]}, {:gallery_name => 'mygallery', :encoding => 'utf-8'})