Skip to content

RestRequest

Scott Offen edited this page May 6, 2014 · 6 revisions

RestRequest is used to interact with a RestClient. It defines the pattern for the resourse, the request method and the request payload content type.

##Constructor## There are multiple constructors that take the following three parameters in any combination, including no parameters at all.

var r = new RestRequest(HttpMethod.GET, "/resource/{id}", ContentType.TXT);

// could also be written

var r = new RestRequest("/resource/{id}", HttpMethod.GET, ContentType.TXT);
  • HttpMethod method : The HTTP request method to be used for the request. Defaults to HttpMethod.GET.

  • ContentType type : The content type of the data being sent. Defaults to ContentType.DEFAULT.

  • String resource : An optionally-tokenized string that represents the path info to the desired resource. Before tokenized requests can be sent, values must be provided for each token. Defaults to an empty string.

##Methods##

###AddParameter (string name, string value)###

A name/value pairs should be added for every token in the resource string, where the name is the same as the string inside the curly braces.

var r = new RestRequest("/{department}/person/{id}");
r.AddParameter("department", "finance");
r.AddParameter("id", "8952-8653345");

When the Reset method is called, all parameters are cleared.

###AddQuery (string name, string value)###

These name/value pairs will be joined into a query string when the QueryString property is requested.

var r = new RestRequest("/person/search");
r.AddQuery("gender", "male");
r.AddQuery("age", "93");

var qstr = r.QueryString;
// returns "gender=male&age=93"
  • You do not need to URI encode the names or the values. Returns null if there are no key/value pairs.

  • When the Reset method is called, all query string values are cleared.

###Reset ()###

The Reset method clears the Payload value and clears all provided parameters and query string key-value pairs. This method is called by default whenever a request is executed, regardless of the response returned.

r.Reset();

##Properties##

###ContentType : ContentType###

Gets or sets the ContentType to be used for this request.

###Encoding : Encoding###

Gets or sets the Encoding to be used for this request.

###Headers : WebHeaderCollection###

Get and/or otherwise interact with the WebHeaderCollection for this client.

All headers are sent with each request.

Headers are not cleared when Reset is called.

###Method : HttpMethod###

Gets or sets the HttpMethod to be used for this request.

###PathInfo : String###

Returns the Resource string with all tokens replaced by their respective values from the AddParameters method.

Throws a ServerStateException if there is not a value for every token in Resource.

###Payload : String###

Gets or sets the data payload to be sent with this request.

###QueryString : String###

Returns the concatenated, URI encoded name/value pairs added via the AddQuery method.

var r = new RestRequest("/person/search");
r.AddQuery("gender", "male");
r.AddQuery("age", "93");

var qstr = r.QueryString;
// returns "gender=male&age=93"

Returns null if there are no name/value pairs.

###Resource : String###

Gets or sets the optionally-tokenized string that represents the path info to the desired resource. When the value is changed, all parameters are cleared (but not the query string).

Clone this wiki locally