Table of Contents

Class RestClientExtensions

Namespace
RestSharp
Assembly
RestSharp.dll
public static class RestClientExtensions
Inheritance
RestClientExtensions
Inherited Members

Methods

AddCookie(RestClient, string, string, string, string)

Adds cookie to the HttpClient cookie container.

public static RestClient AddCookie(this RestClient client, string name, string value, string path, string domain)

Parameters

client RestClient
name string

Cookie name

value string

Cookie value

path string

Cookie path

domain string

Cookie domain, must not be an empty string

Returns

RestClient

AddDefaultHeader(RestClient, string, string)

Adds a default header to the RestClient. Used on every request made by this client instance.

public static RestClient AddDefaultHeader(this RestClient client, string name, string value)

Parameters

client RestClient

RestClientOptions instance

name string

Name of the header to add

value string

Value of the header to add

Returns

RestClient

AddDefaultHeaders(RestClient, Dictionary<string, string>)

Adds default headers to the RestClient. Used on every request made by this client instance.

public static RestClient AddDefaultHeaders(this RestClient client, Dictionary<string, string> headers)

Parameters

client RestClient

RestClientOptions instance

headers Dictionary<string, string>

Dictionary containing the Names and Values of the headers to add

Returns

RestClient

AddDefaultParameter(RestClient, string, object, ParameterType)

Adds a default parameter to the client options. There are four types of parameters:

  • GetOrPost: Either a QueryString value or encoded form value based on method
  • HttpHeader: Adds the name/value pair to the HTTP request's Headers collection
  • UrlSegment: Inserted into URL if there is a matching url token e.g. {AccountId}
  • RequestBody: Used by AddBody() (not recommended to use directly) Used on every request made by this client instance
public static RestClient AddDefaultParameter(this RestClient client, string name, object value, ParameterType type)

Parameters

client RestClient

RestClientOptions instance

name string

Name of the parameter

value object

Value of the parameter

type ParameterType

The type of parameter to add

Returns

RestClient

This request

AddDefaultParameter(RestClient, string, string)

Adds a default HTTP parameter (QueryString for GET, DELETE, OPTIONS and HEAD; Encoded form for POST and PUT) Used on every request made by this client instance

public static RestClient AddDefaultParameter(this RestClient client, string name, string value)

Parameters

client RestClient

RestClientOptions instance

name string

Name of the parameter

value string

Value of the parameter

Returns

RestClient

This request

AddDefaultQueryParameter(RestClient, string, string)

Adds a default URL query parameter to the RestClient. Used on every request made by this client instance.

public static RestClient AddDefaultQueryParameter(this RestClient client, string name, string value)

Parameters

client RestClient

RestClientOptions instance

name string

Name of the query parameter to add

value string

Value of the query parameter to add

Returns

RestClient

AddDefaultUrlSegment(RestClient, string, string)

Adds a default URL segment parameter to the RestClient. Used on every request made by this client instance.

public static RestClient AddDefaultUrlSegment(this RestClient client, string name, string value)

Parameters

client RestClient

RestClientOptions instance

name string

Name of the segment to add

value string

Value of the segment to add

Returns

RestClient

DeleteAsync(RestClient, RestRequest, CancellationToken)

public static Task<RestResponse> DeleteAsync(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)

Parameters

client RestClient
request RestRequest
cancellationToken CancellationToken

Returns

Task<RestResponse>

DeleteAsync<T>(RestClient, RestRequest, CancellationToken)

Execute the request using DELETE HTTP method. Exception will be thrown if the request does not succeed. The response data is deserialized to the Data property of the returned response object.

public static Task<T?> DeleteAsync<T>(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)

Parameters

client RestClient

RestClient instance

request RestRequest

The request

cancellationToken CancellationToken

Cancellation token

Returns

Task<T>

Type Parameters

T

Expected result type

Deserialize<T>(RestClient, RestResponse)

public static RestResponse<T> Deserialize<T>(this RestClient client, RestResponse response)

Parameters

client RestClient
response RestResponse

Returns

RestResponse<T>

Type Parameters

T

DownloadDataAsync(RestClient, RestRequest, CancellationToken)

A specialized method to download files.

public static Task<byte[]?> DownloadDataAsync(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)

Parameters

client RestClient

RestClient instance

request RestRequest

Pre-configured request instance.

cancellationToken CancellationToken

Returns

Task<byte[]>

The downloaded file.

ExecuteAsync(RestClient, RestRequest, Method, CancellationToken)

Executes the request asynchronously, authenticating if needed

public static Task<RestResponse> ExecuteAsync(this RestClient client, RestRequest request, Method httpMethod, CancellationToken cancellationToken = default)

Parameters

client RestClient
request RestRequest

Request to be executed

httpMethod Method

Override the request method

cancellationToken CancellationToken

Cancellation token

Returns

Task<RestResponse>

ExecuteAsync<T>(RestClient, RestRequest, Method, CancellationToken)

Executes the request asynchronously, authenticating if needed

public static Task<RestResponse<T>> ExecuteAsync<T>(this RestClient client, RestRequest request, Method httpMethod, CancellationToken cancellationToken = default)

Parameters

client RestClient
request RestRequest

Request to be executed

httpMethod Method

Override the request method

cancellationToken CancellationToken

Cancellation token

Returns

Task<RestResponse<T>>

Type Parameters

T

Target deserialization type

ExecuteAsync<T>(RestClient, RestRequest, CancellationToken)

Executes the request asynchronously, authenticating if needed

public static Task<RestResponse<T>> ExecuteAsync<T>(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)

Parameters

client RestClient
request RestRequest

Request to be executed

cancellationToken CancellationToken

Cancellation token

Returns

Task<RestResponse<T>>

Type Parameters

T

Target deserialization type

ExecuteGetAsync(RestClient, RestRequest, CancellationToken)

Executes a GET-style asynchronously, authenticating if needed

public static Task<RestResponse> ExecuteGetAsync(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)

Parameters

client RestClient
request RestRequest

Request to be executed

cancellationToken CancellationToken

Cancellation token

Returns

Task<RestResponse>

ExecuteGetAsync<T>(RestClient, RestRequest, CancellationToken)

Executes a GET-style request asynchronously, authenticating if needed. The response content then gets deserialized to T.

public static Task<RestResponse<T>> ExecuteGetAsync<T>(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)

Parameters

client RestClient
request RestRequest

Request to be executed

cancellationToken CancellationToken

Cancellation token

Returns

Task<RestResponse<T>>

Deserialized response content

Type Parameters

T

Target deserialization type

ExecutePostAsync(RestClient, RestRequest, CancellationToken)

Executes a POST-style asynchronously, authenticating if needed

public static Task<RestResponse> ExecutePostAsync(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)

Parameters

client RestClient
request RestRequest

Request to be executed

cancellationToken CancellationToken

Cancellation token

Returns

Task<RestResponse>

ExecutePostAsync<T>(RestClient, RestRequest, CancellationToken)

Executes a POST-style request asynchronously, authenticating if needed. The response content then gets deserialized to T.

public static Task<RestResponse<T>> ExecutePostAsync<T>(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)

Parameters

client RestClient
request RestRequest

Request to be executed

cancellationToken CancellationToken

The cancellation token

Returns

Task<RestResponse<T>>

Deserialized response content

Type Parameters

T

Target deserialization type

ExecutePutAsync(RestClient, RestRequest, CancellationToken)

Executes a PUP-style asynchronously, authenticating if needed

public static Task<RestResponse> ExecutePutAsync(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)

Parameters

client RestClient
request RestRequest

Request to be executed

cancellationToken CancellationToken

Cancellation token

Returns

Task<RestResponse>

ExecutePutAsync<T>(RestClient, RestRequest, CancellationToken)

Executes a PUT-style request asynchronously, authenticating if needed. The response content then gets deserialized to T.

public static Task<RestResponse<T>> ExecutePutAsync<T>(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)

Parameters

client RestClient
request RestRequest

Request to be executed

cancellationToken CancellationToken

The cancellation token

Returns

Task<RestResponse<T>>

Deserialized response content

Type Parameters

T

Target deserialization type

GetAsync(RestClient, RestRequest, CancellationToken)

public static Task<RestResponse> GetAsync(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)

Parameters

client RestClient
request RestRequest
cancellationToken CancellationToken

Returns

Task<RestResponse>

GetAsync<T>(RestClient, RestRequest, CancellationToken)

Execute the request using GET HTTP method. Exception will be thrown if the request does not succeed. The response data is deserialized to the Data property of the returned response object.

public static Task<T?> GetAsync<T>(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)

Parameters

client RestClient

RestClient instance

request RestRequest

The request

cancellationToken CancellationToken

Cancellation token

Returns

Task<T>

Type Parameters

T

Expected result type

GetJsonAsync<TResponse>(RestClient, string, object, CancellationToken)

Calls the URL specified in the

resource
parameter, expecting a JSON response back. Deserializes and returns the response.
public static Task<TResponse?> GetJsonAsync<TResponse>(this RestClient client, string resource, object parameters, CancellationToken cancellationToken = default)

Parameters

client RestClient

RestClient instance

resource string

Resource URL

parameters object

Parameters to pass to the request

cancellationToken CancellationToken

Cancellation token

Returns

Task<TResponse>

Deserialized response object

Type Parameters

TResponse

Response object type

GetJsonAsync<TResponse>(RestClient, string, CancellationToken)

Calls the URL specified in the

resource
parameter, expecting a JSON response back. Deserializes and returns the response.
public static Task<TResponse?> GetJsonAsync<TResponse>(this RestClient client, string resource, CancellationToken cancellationToken = default)

Parameters

client RestClient

RestClient instance

resource string

Resource URL

cancellationToken CancellationToken

Cancellation token

Returns

Task<TResponse>

Type Parameters

TResponse

Response object type

GetJson<TResponse>(RestClient, string)

Calls the URL specified in the

resource
parameter, expecting a JSON response back. Deserializes and returns the response.
public static TResponse? GetJson<TResponse>(this RestClient client, string resource)

Parameters

client RestClient

RestClient instance

resource string

Resource URL

Returns

TResponse

Deserialized response object

Type Parameters

TResponse

Response object type

GetJson<TResponse>(RestClient, string, object)

Calls the URL specified in the

resource
parameter, expecting a JSON response back. Deserializes and returns the response.
public static TResponse? GetJson<TResponse>(this RestClient client, string resource, object parameters)

Parameters

client RestClient

RestClient instance

resource string

Resource URL

parameters object

Parameters to pass to the request

Returns

TResponse

Deserialized response object

Type Parameters

TResponse

Response object type

HeadAsync(RestClient, RestRequest, CancellationToken)

public static Task<RestResponse> HeadAsync(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)

Parameters

client RestClient
request RestRequest
cancellationToken CancellationToken

Returns

Task<RestResponse>

HeadAsync<T>(RestClient, RestRequest, CancellationToken)

Execute the request using HEAD HTTP method. Exception will be thrown if the request does not succeed. The response data is deserialized to the Data property of the returned response object.

public static Task<T?> HeadAsync<T>(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)

Parameters

client RestClient

RestClient instance

request RestRequest

The request

cancellationToken CancellationToken

Cancellation token

Returns

Task<T>

Type Parameters

T

Expected result type

OptionsAsync(RestClient, RestRequest, CancellationToken)

public static Task<RestResponse> OptionsAsync(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)

Parameters

client RestClient
request RestRequest
cancellationToken CancellationToken

Returns

Task<RestResponse>

OptionsAsync<T>(RestClient, RestRequest, CancellationToken)

Execute the request using OPTIONS HTTP method. Exception will be thrown if the request does not succeed. The response data is deserialized to the Data property of the returned response object.

public static Task<T?> OptionsAsync<T>(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)

Parameters

client RestClient

RestClient instance

request RestRequest

The request

cancellationToken CancellationToken

Cancellation token

Returns

Task<T>

Type Parameters

T

Expected result type

PatchAsync(RestClient, RestRequest, CancellationToken)

public static Task<RestResponse> PatchAsync(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)

Parameters

client RestClient
request RestRequest
cancellationToken CancellationToken

Returns

Task<RestResponse>

PatchAsync<T>(RestClient, RestRequest, CancellationToken)

Execute the request using PATCH HTTP method. Exception will be thrown if the request does not succeed. The response data is deserialized to the Data property of the returned response object.

public static Task<T?> PatchAsync<T>(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)

Parameters

client RestClient

RestClient instance

request RestRequest

The request

cancellationToken CancellationToken

Cancellation token

Returns

Task<T>

Type Parameters

T

Expected result type

Post(RestClient, RestRequest)

public static RestResponse Post(this RestClient client, RestRequest request)

Parameters

client RestClient
request RestRequest

Returns

RestResponse

PostAsync(RestClient, RestRequest, CancellationToken)

public static Task<RestResponse> PostAsync(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)

Parameters

client RestClient
request RestRequest
cancellationToken CancellationToken

Returns

Task<RestResponse>

PostAsync<T>(RestClient, RestRequest, CancellationToken)

Execute the request using POST HTTP method. Exception will be thrown if the request does not succeed. The response data is deserialized to the Data property of the returned response object.

public static Task<T?> PostAsync<T>(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)

Parameters

client RestClient

RestClient instance

request RestRequest

The request

cancellationToken CancellationToken

Cancellation token

Returns

Task<T>

Type Parameters

T

Expected result type

PostJsonAsync<TRequest>(RestClient, string, TRequest, CancellationToken)

Serializes the

request
object to JSON and makes a POST call to the resource specified in the
resource
parameter. Expects no response back, just the status code.
public static Task<HttpStatusCode> PostJsonAsync<TRequest>(this RestClient client, string resource, TRequest request, CancellationToken cancellationToken = default) where TRequest : class

Parameters

client RestClient

RestClient instance

resource string

Resource URL

request TRequest

Request object, must be serializable to JSON

cancellationToken CancellationToken

Cancellation token

Returns

Task<HttpStatusCode>

Response status code

Type Parameters

TRequest

Request object type

PostJsonAsync<TRequest, TResponse>(RestClient, string, TRequest, CancellationToken)

Serializes the

request
object to JSON and makes a POST call to the resource specified in the
resource
parameter. Expects a JSON response back, deserializes it to
TResponse
type and returns it.
public static Task<TResponse?> PostJsonAsync<TRequest, TResponse>(this RestClient client, string resource, TRequest request, CancellationToken cancellationToken = default) where TRequest : class

Parameters

client RestClient

RestClient instance

resource string

Resource URL

request TRequest

Request object, must be serializable to JSON

cancellationToken CancellationToken

Cancellation token

Returns

Task<TResponse>

Deserialized response object

Type Parameters

TRequest

Request object type

TResponse

Response object type

PostJson<TRequest>(RestClient, string, TRequest)

Serializes the

request
object to JSON and makes a POST call to the resource specified in the
resource
parameter. Expects no response back, just the status code.
public static HttpStatusCode PostJson<TRequest>(this RestClient client, string resource, TRequest request) where TRequest : class

Parameters

client RestClient

RestClient instance

resource string

Resource URL

request TRequest

Request object, must be serializable to JSON

Returns

HttpStatusCode

Response status code

Type Parameters

TRequest

Request object type

PostJson<TRequest, TResponse>(RestClient, string, TRequest)

Serializes the

request
object to JSON and makes a POST call to the resource specified in the
resource
parameter. Expects a JSON response back, deserializes it to
TResponse
type and returns it.
public static TResponse? PostJson<TRequest, TResponse>(this RestClient client, string resource, TRequest request) where TRequest : class

Parameters

client RestClient

RestClient instance

resource string

Resource URL

request TRequest

Request object, must be serializable to JSON

Returns

TResponse

Deserialized response object

Type Parameters

TRequest

Request object type

TResponse

Response object type

PutAsync(RestClient, RestRequest, CancellationToken)

public static Task<RestResponse> PutAsync(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)

Parameters

client RestClient
request RestRequest
cancellationToken CancellationToken

Returns

Task<RestResponse>

PutAsync<T>(RestClient, RestRequest, CancellationToken)

Execute the request using PUT HTTP method. Exception will be thrown if the request does not succeed. The response data is deserialized to the Data property of the returned response object.

public static Task<T?> PutAsync<T>(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)

Parameters

client RestClient

RestClient instance

request RestRequest

The request

cancellationToken CancellationToken

Cancellation token

Returns

Task<T>

Type Parameters

T

Expected result type

PutJsonAsync<TRequest>(RestClient, string, TRequest, CancellationToken)

Serializes the

request
object to JSON and makes a PUT call to the resource specified in the
resource
parameter. Expects no response back, just the status code.
public static Task<HttpStatusCode> PutJsonAsync<TRequest>(this RestClient client, string resource, TRequest request, CancellationToken cancellationToken = default) where TRequest : class

Parameters

client RestClient

RestClient instance

resource string

Resource URL

request TRequest

Request object, must be serializable to JSON

cancellationToken CancellationToken

Cancellation token

Returns

Task<HttpStatusCode>

Response status code

Type Parameters

TRequest

Request object type

PutJsonAsync<TRequest, TResponse>(RestClient, string, TRequest, CancellationToken)

Serializes the

request
object to JSON and makes a PUT call to the resource specified in the
resource
parameter. Expects a JSON response back, deserializes it to
TResponse
type and returns it.
public static Task<TResponse?> PutJsonAsync<TRequest, TResponse>(this RestClient client, string resource, TRequest request, CancellationToken cancellationToken = default) where TRequest : class

Parameters

client RestClient

RestClient instance

resource string

Resource URL

request TRequest

Request object, must be serializable to JSON

cancellationToken CancellationToken

Cancellation token

Returns

Task<TResponse>

Deserialized response object

Type Parameters

TRequest

Request object type

TResponse

Response object type

PutJson<TRequest>(RestClient, string, TRequest)

Serializes the

request
object to JSON and makes a PUT call to the resource specified in the
resource
parameter. Expects no response back, just the status code.
public static HttpStatusCode PutJson<TRequest>(this RestClient client, string resource, TRequest request) where TRequest : class

Parameters

client RestClient

RestClient instance

resource string

Resource URL

request TRequest

Request object, must be serializable to JSON

Returns

HttpStatusCode

Response status code

Type Parameters

TRequest

Request object type

PutJson<TRequest, TResponse>(RestClient, string, TRequest)

Serializes the

request
object to JSON and makes a PUT call to the resource specified in the
resource
parameter. Expects a JSON response back, deserializes it to
TResponse
type and returns it.
public static TResponse? PutJson<TRequest, TResponse>(this RestClient client, string resource, TRequest request) where TRequest : class

Parameters

client RestClient

RestClient instance

resource string

Resource URL

request TRequest

Request object, must be serializable to JSON

Returns

TResponse

Deserialized response object

Type Parameters

TRequest

Request object type

TResponse

Response object type

StreamJsonAsync<T>(RestClient, string, CancellationToken)

Reads a stream returned by the specified endpoint, deserializes each line to JSON and returns each object asynchronously. It is required for each JSON object to be returned in a single line.

public static IAsyncEnumerable<T> StreamJsonAsync<T>(this RestClient client, string resource, CancellationToken cancellationToken)

Parameters

client RestClient
resource string
cancellationToken CancellationToken

Returns

IAsyncEnumerable<T>

Type Parameters

T

UseAuthenticator(RestClient, IAuthenticator)

public static RestClient UseAuthenticator(this RestClient client, IAuthenticator authenticator)

Parameters

client RestClient
authenticator IAuthenticator

Returns

RestClient

UseJson(RestClient)

Sets the RestClient to only use JSON

public static RestClient UseJson(this RestClient client)

Parameters

client RestClient

Client instance to work with

Returns

RestClient

Reference to the client instance

UseOnlySerializer(RestClient, Func<IRestSerializer>)

Sets the RestClient to only use the passed in custom serializer

public static RestClient UseOnlySerializer(this RestClient client, Func<IRestSerializer> serializerFactory)

Parameters

client RestClient

Client instance to work with

serializerFactory Func<IRestSerializer>

Function that returns the serializer instance

Returns

RestClient

Reference to the client instance

UseQueryEncoder(RestClient, Func<string, Encoding, string>)

Allows to use a custom way to encode query parameters

public static RestClient UseQueryEncoder(this RestClient client, Func<string, Encoding, string> queryEncoder)

Parameters

client RestClient
queryEncoder Func<string, Encoding, string>

A delegate to encode query parameters

Returns

RestClient

Examples

client.UseUrlEncoder((s, encoding) => HttpUtility.UrlEncode(s, encoding));

UseUrlEncoder(RestClient, Func<string, string>)

Allows to use a custom way to encode URL parameters

public static RestClient UseUrlEncoder(this RestClient client, Func<string, string> encoder)

Parameters

client RestClient
encoder Func<string, string>

A delegate to encode URL parameters

Returns

RestClient

Examples

client.UseUrlEncoder(s => HttpUtility.UrlEncode(s));

UseXml(RestClient)

Sets the RestClient to only use XML

public static RestClient UseXml(this RestClient client)

Parameters

client RestClient

Client instance to work with

Returns

RestClient

Reference to the client instance