FormUrlEncodedHandler
The FormUrlEncodedHandler allows you to post using “application/x-www-form-urlencoded” data.
To use it simply call the UseFormUrlEncodedHandler() extension method and set the header Content-Type to “application/x-www-form-urlencoded” and use as normal.
var config = new Config() //Build a pipeline using extension methods
.UseFormUrlEncodedHandler();
dynamic restClient = new RestClient("http://jsonplaceholder.typicode.com", config);
var user = new User { name="foo", email="foo@bar.com" };
//PUT name=foo&email=foo@bar.com http://jsonplaceholder.typicode.com/users/1
var result = await client
.Headers(new Headers { { "Content-Type", "application/x-www-form-urlencoded" }})
.User(1)
.Put(user);
Simple Array Example
var config = new Config() //Build a pipeline using extension methods
.UseFormUrlEncodedHandler();
dynamic restClient = new RestClient("http://jsonplaceholder.typicode.com", config);
var user = new User { name="foo", emailAddresses=new[] {"foo@bar.com", "x@x.com"} };
//PUT name=foo&emailAddresses=foo@bar.com&emailAddresses=x@x.com http://jsonplaceholder.typicode.com/users/1
var result = await client
.Headers(new Headers { { "Content-Type", "application/x-www-form-urlencoded" }})
.User(1)
.Put(user);
Complex Array Example
var config = new Config() //Build a pipeline using extension methods
.UseFormUrlEncodedHandler();
dynamic restClient = new RestClient("http://jsonplaceholder.typicode.com", config);
var users = new
{
user = new[]
{
new { FirstName = "George", LastName = "Washington" },
new { FirstName = "Abraham", LastName = "Lincoln" }
}
};
//POST user%5B0%5D.FirstName=George&user%5B0%5D.LastName=Washington&user%5B1%5D.FirstName=Abraham&user%5B1%5D.LastName=Lincoln http://jsonplaceholder.typicode.com/users
var result = await client
.Headers(new Headers { { "Content-Type", "application/x-www-form-urlencoded" }})
.Users
.Post(users);
Nested Object Example
var config = new Config() //Build a pipeline using extension methods
.UseFormUrlEncodedHandler();
dynamic restClient = new RestClient("http://jsonplaceholder.typicode.com", config);
var user = new
{
user = new { name="foo", email="foo@bar.com" }
};
//PUT user.name=foo&user.email=foo@bar.com http://jsonplaceholder.typicode.com/users/1
var result = await client
.Headers(new Headers { { "Content-Type", "application/x-www-form-urlencoded" }})
.User(1)
.Put(user);