Class MultipartFormData
In: lib/multipart_form_data.rb
Parent: Object

Synopsis

MultipartFormData facilitates in building multipart/form-data encoded messages that can be POSTed using ThinHTTP. This class is a simple wrapper around the MIME::MultipartMedia::FormData class.

Caveats

According to RFC 1867 and 2388, each field of the form is to be sent in the order in which it occurs in the form. However, the Hash containing the form fields inherently does not maintain order. This is probably not a problem.

Examples

Simple usage of the class

The following params Hash:

  params = {
    'text_field' => 'this is some text',
    'image_field' => Pathname.new('/tmp/pic.jpg')
  }

simulates the following HTML form:

  <form>
    <input type="text" name="text_field"/>
    <input type="file" name="image_field"/>
  </form>

Creating and using a MultipartFormData instance initialized with params:

  fd = MultipartFormData.new(params)
  fd.content_type   # outputs the content type header including boundary
  fd.content        # outputs the content (multipart/form-data encoded entities)

Constructing a multipart/form-data message and POSTing it via ThinHTTP

  form_data = MultipartFormData.new(
    :dog_owner => 'Mary Jane',
    :pic_comment => 'These are my two black lab/pit mix puppies.',
    :dog_pic1 => Pathname.new('/tmp/lexi.jpg'),
    :dog_pic2 => Pathname.new('/tmp/simone.jpg')
  )

  th = ThinHTTP.new('petfotoz.com', 80)
  th.post('/photo_album.cgi', form_data, form_data.content_type)

Methods

content   new   to_s  

Attributes

content_type  [R] 

Public Class methods

Returns new MultipartFormData instance initialized with params. params is a Hash of key/value pairs representing HTML input variable names and values. For HTML file type inputs, use a Pathname object.

Public Instance methods

content()

Alias for to_s

Return the multipart/form-data content.

[Validate]