2014-08-23 16:16:09 +09:00
|
|
|
require "net/http"
|
|
|
|
require "mime/types"
|
2010-04-20 23:05:11 +00:00
|
|
|
|
|
|
|
class Net::HTTP::Post
|
2014-08-23 16:44:43 +09:00
|
|
|
def multipart=(params = [])
|
2010-04-20 23:05:11 +00:00
|
|
|
boundary_token = "--multipart-boundary"
|
|
|
|
self.content_type = "multipart/form-data; boundary=#{boundary_token}"
|
|
|
|
|
|
|
|
self.body = ""
|
2014-08-23 18:10:14 +09:00
|
|
|
params.each do |p|
|
2010-04-20 23:05:11 +00:00
|
|
|
self.body += "--#{boundary_token}\r\n"
|
|
|
|
self.body += "Content-Disposition: form-data; name=#{p[:name]}"
|
|
|
|
self.body += "; filename=#{p[:filename]}" if p[:filename]
|
|
|
|
self.body += "\r\n"
|
2014-11-08 22:57:37 +09:00
|
|
|
if p[:binary]
|
2010-04-20 23:05:11 +00:00
|
|
|
self.body += "Content-Transfer-Encoding: binary\r\n"
|
|
|
|
|
|
|
|
mime_type = "application/octet-stream"
|
|
|
|
if p[:filename]
|
|
|
|
mime_types = MIME::Types.of(p[:filename])
|
|
|
|
mime_type = mime_types.first.content_type unless mime_types.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
self.body += "Content-Type: #{mime_type}\r\n"
|
|
|
|
end
|
2014-08-23 20:09:48 +09:00
|
|
|
self.body += "\r\n#{p[:data]}\r\n"
|
2014-08-23 18:10:14 +09:00
|
|
|
end
|
2010-04-20 23:05:11 +00:00
|
|
|
self.body += "--#{boundary_token}--\r\n"
|
|
|
|
end
|
|
|
|
end
|