Module: Fedora::Multipart
- Included in:
- Net::HTTP::Post, Net::HTTP::Put
- Defined in:
- lib/fedora/connection.rb
Overview
Add multipart/form-data support to net/http
Usage
File.open(File.expand_path(‘script/test.png’), ‘r’) do |file|
http = Net::HTTP.new('localhost', 3000)
begin
http.start do |http|
request = Net::HTTP::Post.new('/your/url/here')
request.set_multipart_data(:file => file, :title => 'test.png')
response = http.request(request)
puts response
end
rescue Net::HTTPServerException => e
p e
end
end
Instance Method Summary collapse
Instance Method Details
#set_multipart_data(param_hash = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/fedora/connection.rb', line 24 def set_multipart_data(param_hash={}) boundary_token = [Array.new(8) {rand(256)}].join self.content_type = "multipart/form-data; boundary=#{boundary_token}" boundary_marker = "--#{boundary_token}\r\n" self.body = param_hash.map { |param_name, param_value| boundary_marker + case param_value when File then file_to_multipart(param_name, param_value) when String then text_to_multipart(param_name, param_value) else "" end }.join('') + "--#{boundary_token}--\r\n" end |