MultipartFormDataHandler

The MultipartFormDataHandler allows you to post using “multipart/form-data” data, which means you can post both form data and files.

To use it simply call the UseMultipartFormDataHandler() extension method and set the header Content-Type to “multipart/form-data” and use as normal. MultipartFormDataHandler works with all mutable verbs (POST, Put and Patch).

Post a file using a stream

The MultipartFormDataHandler will detect any properties in your Post object that have there values set to stream, it will add it them to the “multipart/form-data” request as you would expect.

Posting multiple files using a stream example

var config = new Config() //Build a pipeline using extension methods
                    .UseMultipartFormDataHandler();

dynamic restClient = new RestClient("http://test.com,", config);
Stream stream1 = new FileStream("c:\DalSoft1.jpg", FileMode.Open, FileAccess.Read);
Stream stream2 = new FileStream("c:\DalSoft2.jpg", FileMode.Open, FileAccess.Read);

var multipartData = new 
{ 
      theFile1 = stream1,
      theFile2 = stream2
};

var result = await client
      .Headers(new Headers { { "Content-Type", "multipart/form-data" }})
      .Post(multipartData);

Post a file using a byte array

The MultipartFormDataHandler will detect any properties in your Post object that have there values set to a byte array, it will add it them to the “multipart/form-data” request as you would expect.

Posting multiple files using a byte array example**

var config = new Config() //Build a pipeline using extension methods
                    .UseMultipartFormDataHandler();

dynamic restClient = new RestClient("http://test.com,", config);
var bytes1 = File.ReadAllBytes("c:\DalSoft1.jpg");
var bytes2 = File.ReadAllBytes("c:\DalSoft2.jpg");

var multipartData = new 
{ 
      theFile1 = bytes1,
      theFile2 = bytes2 
};

var result = await client
      .Headers(new Headers { { "Content-Type", "multipart/form-data" }})
      .Post(multipartData);

Set boundary

Nothing special here - to set the boundary simply add it to the Content-Type as you would normally would.

var result = await client
      .Headers(new Headers { { "Content-Type", "multipart/form-data;boundary=\"YOUR-BOUNDARY-HERE\"" }})
      .Post(multipartData);

Set FileName

If you want to add FileName to the “multipart/form-data” request, just add a FileName property to your Post object.

var multipartData = new 
{ 
      myFile = bytes,
      fileName = "DalSoft.jpg"
};

var result = await client
      .Headers(new Dictionary<string, string> { { "Content-Type", "multipart/form-data" }})
      .Post(multipartData);

Mixing file and form data

As you would expect MultipartFormDataHandler supports both file and form data in the same request. This works by just adding the properties you require to Post object as you normally would.

var config = new Config() //Build a pipeline using extension methods
                    .UseMultipartFormDataHandler();

dynamic restClient = new RestClient("http://test.com,", config);
var bytes = File.ReadAllBytes("c:\DalSoft1.jpg");

var multipartData = new 
{ 
      myFile = bytes,
      Company = "DalSoft",
      Country = "United Kingdom"
};

var result = await client
      .Headers(new Dictionary<string, string> { { "Content-Type", "multipart/form-data" }})
      .Post(multipartData);

Nested objects and simple/complex arrays are supported in exactly the same way as in FormUrlEncodedHandler