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
clientRestClientnamestringCookie name
valuestringCookie value
pathstringCookie path
domainstringCookie domain, must not be an empty string
Returns
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
clientRestClientRestClientOptions instance
namestringName of the header to add
valuestringValue of the header to add
Returns
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
clientRestClientRestClientOptions instance
headersDictionary<string, string>Dictionary containing the Names and Values of the headers to add
Returns
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
clientRestClientRestClientOptions instance
namestringName of the parameter
valueobjectValue of the parameter
typeParameterTypeThe 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
clientRestClientRestClientOptions instance
namestringName of the parameter
valuestringValue 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
clientRestClientRestClientOptions instance
namestringName of the query parameter to add
valuestringValue of the query parameter to add
Returns
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
clientRestClientRestClientOptions instance
namestringName of the segment to add
valuestringValue of the segment to add
Returns
DeleteAsync(RestClient, RestRequest, CancellationToken)
public static Task<RestResponse> DeleteAsync(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)
Parameters
clientRestClientrequestRestRequestcancellationTokenCancellationToken
Returns
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
clientRestClientRestClient instance
requestRestRequestThe request
cancellationTokenCancellationTokenCancellation token
Returns
- Task<T>
Type Parameters
TExpected result type
Deserialize<T>(RestClient, RestResponse)
public static RestResponse<T> Deserialize<T>(this RestClient client, RestResponse response)
Parameters
clientRestClientresponseRestResponse
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
clientRestClientRestClient instance
requestRestRequestPre-configured request instance.
cancellationTokenCancellationToken
Returns
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
clientRestClientrequestRestRequestRequest to be executed
httpMethodMethodOverride the request method
cancellationTokenCancellationTokenCancellation token
Returns
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
clientRestClientrequestRestRequestRequest to be executed
httpMethodMethodOverride the request method
cancellationTokenCancellationTokenCancellation token
Returns
- Task<RestResponse<T>>
Type Parameters
TTarget 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
clientRestClientrequestRestRequestRequest to be executed
cancellationTokenCancellationTokenCancellation token
Returns
- Task<RestResponse<T>>
Type Parameters
TTarget 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
clientRestClientrequestRestRequestRequest to be executed
cancellationTokenCancellationTokenCancellation token
Returns
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
clientRestClientrequestRestRequestRequest to be executed
cancellationTokenCancellationTokenCancellation token
Returns
- Task<RestResponse<T>>
Deserialized response content
Type Parameters
TTarget 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
clientRestClientrequestRestRequestRequest to be executed
cancellationTokenCancellationTokenCancellation token
Returns
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
clientRestClientrequestRestRequestRequest to be executed
cancellationTokenCancellationTokenThe cancellation token
Returns
- Task<RestResponse<T>>
Deserialized response content
Type Parameters
TTarget 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
clientRestClientrequestRestRequestRequest to be executed
cancellationTokenCancellationTokenCancellation token
Returns
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
clientRestClientrequestRestRequestRequest to be executed
cancellationTokenCancellationTokenThe cancellation token
Returns
- Task<RestResponse<T>>
Deserialized response content
Type Parameters
TTarget deserialization type
GetAsync(RestClient, RestRequest, CancellationToken)
public static Task<RestResponse> GetAsync(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)
Parameters
clientRestClientrequestRestRequestcancellationTokenCancellationToken
Returns
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
clientRestClientRestClient instance
requestRestRequestThe request
cancellationTokenCancellationTokenCancellation token
Returns
- Task<T>
Type Parameters
TExpected 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
clientRestClientRestClient instance
resourcestringResource URL
parametersobjectParameters to pass to the request
cancellationTokenCancellationTokenCancellation token
Returns
- Task<TResponse>
Deserialized response object
Type Parameters
TResponseResponse 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
clientRestClientRestClient instance
resourcestringResource URL
cancellationTokenCancellationTokenCancellation token
Returns
- Task<TResponse>
Type Parameters
TResponseResponse 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
clientRestClientRestClient instance
resourcestringResource URL
Returns
- TResponse
Deserialized response object
Type Parameters
TResponseResponse 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
clientRestClientRestClient instance
resourcestringResource URL
parametersobjectParameters to pass to the request
Returns
- TResponse
Deserialized response object
Type Parameters
TResponseResponse object type
HeadAsync(RestClient, RestRequest, CancellationToken)
public static Task<RestResponse> HeadAsync(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)
Parameters
clientRestClientrequestRestRequestcancellationTokenCancellationToken
Returns
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
clientRestClientRestClient instance
requestRestRequestThe request
cancellationTokenCancellationTokenCancellation token
Returns
- Task<T>
Type Parameters
TExpected result type
OptionsAsync(RestClient, RestRequest, CancellationToken)
public static Task<RestResponse> OptionsAsync(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)
Parameters
clientRestClientrequestRestRequestcancellationTokenCancellationToken
Returns
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
clientRestClientRestClient instance
requestRestRequestThe request
cancellationTokenCancellationTokenCancellation token
Returns
- Task<T>
Type Parameters
TExpected result type
PatchAsync(RestClient, RestRequest, CancellationToken)
public static Task<RestResponse> PatchAsync(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)
Parameters
clientRestClientrequestRestRequestcancellationTokenCancellationToken
Returns
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
clientRestClientRestClient instance
requestRestRequestThe request
cancellationTokenCancellationTokenCancellation token
Returns
- Task<T>
Type Parameters
TExpected result type
Post(RestClient, RestRequest)
public static RestResponse Post(this RestClient client, RestRequest request)
Parameters
clientRestClientrequestRestRequest
Returns
PostAsync(RestClient, RestRequest, CancellationToken)
public static Task<RestResponse> PostAsync(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)
Parameters
clientRestClientrequestRestRequestcancellationTokenCancellationToken
Returns
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
clientRestClientRestClient instance
requestRestRequestThe request
cancellationTokenCancellationTokenCancellation token
Returns
- Task<T>
Type Parameters
TExpected 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
clientRestClientRestClient instance
resourcestringResource URL
requestTRequestRequest object, must be serializable to JSON
cancellationTokenCancellationTokenCancellation token
Returns
- Task<HttpStatusCode>
Response status code
Type Parameters
TRequestRequest 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
clientRestClientRestClient instance
resourcestringResource URL
requestTRequestRequest object, must be serializable to JSON
cancellationTokenCancellationTokenCancellation token
Returns
- Task<TResponse>
Deserialized response object
Type Parameters
TRequestRequest object type
TResponseResponse 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
clientRestClientRestClient instance
resourcestringResource URL
requestTRequestRequest object, must be serializable to JSON
Returns
- HttpStatusCode
Response status code
Type Parameters
TRequestRequest 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
clientRestClientRestClient instance
resourcestringResource URL
requestTRequestRequest object, must be serializable to JSON
Returns
- TResponse
Deserialized response object
Type Parameters
TRequestRequest object type
TResponseResponse object type
PutAsync(RestClient, RestRequest, CancellationToken)
public static Task<RestResponse> PutAsync(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)
Parameters
clientRestClientrequestRestRequestcancellationTokenCancellationToken
Returns
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
clientRestClientRestClient instance
requestRestRequestThe request
cancellationTokenCancellationTokenCancellation token
Returns
- Task<T>
Type Parameters
TExpected 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
clientRestClientRestClient instance
resourcestringResource URL
requestTRequestRequest object, must be serializable to JSON
cancellationTokenCancellationTokenCancellation token
Returns
- Task<HttpStatusCode>
Response status code
Type Parameters
TRequestRequest 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
clientRestClientRestClient instance
resourcestringResource URL
requestTRequestRequest object, must be serializable to JSON
cancellationTokenCancellationTokenCancellation token
Returns
- Task<TResponse>
Deserialized response object
Type Parameters
TRequestRequest object type
TResponseResponse 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
clientRestClientRestClient instance
resourcestringResource URL
requestTRequestRequest object, must be serializable to JSON
Returns
- HttpStatusCode
Response status code
Type Parameters
TRequestRequest 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
clientRestClientRestClient instance
resourcestringResource URL
requestTRequestRequest object, must be serializable to JSON
Returns
- TResponse
Deserialized response object
Type Parameters
TRequestRequest object type
TResponseResponse 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
clientRestClientresourcestringcancellationTokenCancellationToken
Returns
Type Parameters
T
UseAuthenticator(RestClient, IAuthenticator)
public static RestClient UseAuthenticator(this RestClient client, IAuthenticator authenticator)
Parameters
clientRestClientauthenticatorIAuthenticator
Returns
UseJson(RestClient)
Sets the RestClient to only use JSON
public static RestClient UseJson(this RestClient client)
Parameters
clientRestClientClient 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
clientRestClientClient instance to work with
serializerFactoryFunc<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
clientRestClientqueryEncoderFunc<string, Encoding, string>A delegate to encode query parameters
Returns
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
clientRestClientencoderFunc<string, string>A delegate to encode URL parameters
Returns
Examples
client.UseUrlEncoder(s => HttpUtility.UrlEncode(s));
UseXml(RestClient)
Sets the RestClient to only use XML
public static RestClient UseXml(this RestClient client)
Parameters
clientRestClientClient instance to work with
Returns
- RestClient
Reference to the client instance