Class: MultipartPost
- Inherits:
-
Object
- Object
- MultipartPost
- Defined in:
- lib/multipart_post.rb
Class Method Summary collapse
-
.post(uri, params = []) ⇒ Object
see www.realityforge.org/articles/2006/03/02/upload-a-file-via-post-with-net-http for file upload with http.
Class Method Details
.post(uri, params = []) ⇒ Object
see www.realityforge.org/articles/2006/03/02/upload-a-file-via-post-with-net-http for file upload with http
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/multipart_post.rb', line 3 def self.post(uri, params=[]) chunks = [] params.each do |param| param[:name] chunks << if param[:mime_type] "Content-Disposition: form-data; name=\"#{CGI::escape(param[:name])}\"; filename=\"#{param[:filename]}\"\r\n" + "Content-Transfer-Encoding: binary\r\n" + "Content-Type: #{param[:mime_type]}\r\n" + "\r\n#{param[:value]}\r\n" else "Content-Disposition: form-data; name=\"#{CGI::escape(param[:name])}\"\r\n" + "\r\n#{param[:value]}\r\n" end end boundary = "349832898984244898448024464570528145" post_body = "" chunks.each do |chunk| post_body << "--#{boundary}\r\n" post_body << chunk end post_body << "--#{boundary}--\r\n" uri = URI.parse(uri) Net::HTTP.new(uri.host, uri.port).start do |http| http.request_post(uri.path, post_body, "Content-type" => "multipart/form-data; boundary=" + boundary) end end |