Class: RestClient::Payload::Multipart

### Home Classes Methods

### In Files

- lib/restclient/payload.rb

### Parent

RestClient::Payload::Base

### Methods

- #boundary
- #build_stream
- #close
- #create_file_field
- #create_regular_field
- #handle_key
- #headers
- #mime_for

### Class/Module Index

Quicksearch

- RestClient
- RestClient::AbstractResponse
- RestClient::Exception
- RestClient::ExceptionWithResponse
- RestClient::Exceptions
- RestClient::MaxRedirectsReached
- RestClient::Payload
- RestClient::Payload::Base
- RestClient::Payload::Multipart
- RestClient::Payload::Streamed
- RestClient::Payload::UrlEncoded
- RestClient::Platform
- RestClient::RawResponse
- RestClient::Redirect
- RestClient::Request
- RestClient::Request::Redirect
- RestClient::Request::RequestFailed
- RestClient::RequestFailed
- RestClient::Resource
- RestClient::Response
- RestClient::ResponseForException
- RestClient::SSLCertificateNotVerified
- RestClient::ServerBrokeConnection
- RestClient::Windows
- RestClient::Windows::RootCerts
- RestClient::Windows::RootCerts::CERT_CONTEXT

No matching classes.

# RestClient::Payload::Multipart

### Constants

EOL

### Public Instance Methods

boundary() click to toggle source

    # File lib/restclient/payload.rb, line 220
    def boundary
      @boundary ||= rand(1_000_000).to_s
    end

build_stream(params) click to toggle source

    # File lib/restclient/payload.rb, line 164
    def build_stream(params)
      b = "--#{boundary}"

      @stream = Tempfile.new("RESTClient.Stream.#{rand(1000)}")
      @stream.binmode
      @stream.write(b + EOL)

      if params.is_a? Hash
        x = flatten_params(params)
      else
        x = params
      end

      last_index = x.length - 1
      x.each_with_index do |a, index|
        k, v = * a
        if v.respond_to?(:read) && v.respond_to?(:path)
          create_file_field(@stream, k, v)
        else
          create_regular_field(@stream, k, v)
        end
        @stream.write(EOL + b)
        @stream.write(EOL) unless last_index == index
      end
      @stream.write('--')
      @stream.write(EOL)
      @stream.seek(0)
    end

close() click to toggle source

    # File lib/restclient/payload.rb, line 233
    def close
      @stream.close!
    end

create_file_field(s, k, v) click to toggle source

    # File lib/restclient/payload.rb, line 200
    def create_file_field(s, k, v)
      begin
        s.write("Content-Disposition: form-data;")
        s.write(" name=\"#{k}\";") unless (k.nil? || k=='')
        s.write(" filename=\"#{v.respond_to?(:original_filename) ? v.original_filename : File.basename(v.path)}\"#{EOL}")
        s.write("Content-Type: #{v.respond_to?(:content_type) ? v.content_type : mime_for(v.path)}#{EOL}")
        s.write(EOL)
        while (data = v.read(8124))
          s.write(data)
        end
      ensure
        v.close if v.respond_to?(:close)
      end
    end

create_regular_field(s, k, v) click to toggle source

    # File lib/restclient/payload.rb, line 193
    def create_regular_field(s, k, v)
      s.write("Content-Disposition: form-data; name=\"#{k}\"")
      s.write(EOL)
      s.write(EOL)
      s.write(v)
    end

handle_key(key) click to toggle source

for Multipart do not escape the keys

    # File lib/restclient/payload.rb, line 225
    def handle_key key
      key
    end

headers() click to toggle source

    # File lib/restclient/payload.rb, line 229
    def headers
      super.merge({'Content-Type' => %{multipart/form-data; boundary=#{boundary}}})
    end

mime_for(path) click to toggle source

    # File lib/restclient/payload.rb, line 215
    def mime_for(path)
      mime = MIME::Types.type_for path
      mime.empty? ? 'text/plain' : mime[0].content_type
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.