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
RestClientname
stringCookie name
value
stringCookie value
path
stringCookie path
domain
stringCookie 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
client
RestClientRestClientOptions instance
name
stringName of the header to add
value
stringValue 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
client
RestClientRestClientOptions instance
headers
Dictionary<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
client
RestClientRestClientOptions instance
name
stringName of the parameter
value
objectValue of the parameter
type
ParameterTypeThe 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
RestClientRestClientOptions instance
name
stringName of the parameter
value
stringValue 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
RestClientRestClientOptions instance
name
stringName of the query parameter to add
value
stringValue 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
client
RestClientRestClientOptions instance
name
stringName of the segment to add
value
stringValue of the segment to add
Returns
DeleteAsync(RestClient, RestRequest, CancellationToken)
public static Task<RestResponse> DeleteAsync(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)
Parameters
client
RestClientrequest
RestRequestcancellationToken
CancellationToken
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
client
RestClientRestClient instance
request
RestRequestThe request
cancellationToken
CancellationTokenCancellation 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
RestClientresponse
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
RestClientRestClient instance
request
RestRequestPre-configured request instance.
cancellationToken
CancellationToken
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
client
RestClientrequest
RestRequestRequest to be executed
httpMethod
MethodOverride the request method
cancellationToken
CancellationTokenCancellation 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
client
RestClientrequest
RestRequestRequest to be executed
httpMethod
MethodOverride the request method
cancellationToken
CancellationTokenCancellation 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
RestClientrequest
RestRequestRequest to be executed
cancellationToken
CancellationTokenCancellation 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
RestClientrequest
RestRequestRequest to be executed
cancellationToken
CancellationTokenCancellation 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
client
RestClientrequest
RestRequestRequest to be executed
cancellationToken
CancellationTokenCancellation 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
RestClientrequest
RestRequestRequest to be executed
cancellationToken
CancellationTokenCancellation 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
client
RestClientrequest
RestRequestRequest to be executed
cancellationToken
CancellationTokenThe 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
RestClientrequest
RestRequestRequest to be executed
cancellationToken
CancellationTokenCancellation 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
client
RestClientrequest
RestRequestRequest to be executed
cancellationToken
CancellationTokenThe 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
RestClientrequest
RestRequestcancellationToken
CancellationToken
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
client
RestClientRestClient instance
request
RestRequestThe request
cancellationToken
CancellationTokenCancellation 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
RestClientRestClient instance
resource
stringResource URL
parameters
objectParameters to pass to the request
cancellationToken
CancellationTokenCancellation 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
RestClientRestClient instance
resource
stringResource URL
cancellationToken
CancellationTokenCancellation 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
RestClientRestClient instance
resource
stringResource 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
RestClientRestClient instance
resource
stringResource URL
parameters
objectParameters 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
RestClientrequest
RestRequestcancellationToken
CancellationToken
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
client
RestClientRestClient instance
request
RestRequestThe request
cancellationToken
CancellationTokenCancellation 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
RestClientrequest
RestRequestcancellationToken
CancellationToken
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
client
RestClientRestClient instance
request
RestRequestThe request
cancellationToken
CancellationTokenCancellation 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
RestClientrequest
RestRequestcancellationToken
CancellationToken
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
client
RestClientRestClient instance
request
RestRequestThe request
cancellationToken
CancellationTokenCancellation token
Returns
- Task<T>
Type Parameters
T
Expected result type
Post(RestClient, RestRequest)
public static RestResponse Post(this RestClient client, RestRequest request)
Parameters
client
RestClientrequest
RestRequest
Returns
PostAsync(RestClient, RestRequest, CancellationToken)
public static Task<RestResponse> PostAsync(this RestClient client, RestRequest request, CancellationToken cancellationToken = default)
Parameters
client
RestClientrequest
RestRequestcancellationToken
CancellationToken
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
client
RestClientRestClient instance
request
RestRequestThe request
cancellationToken
CancellationTokenCancellation 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
RestClientRestClient instance
resource
stringResource URL
request
TRequestRequest object, must be serializable to JSON
cancellationToken
CancellationTokenCancellation 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
RestClientRestClient instance
resource
stringResource URL
request
TRequestRequest object, must be serializable to JSON
cancellationToken
CancellationTokenCancellation 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
RestClientRestClient instance
resource
stringResource URL
request
TRequestRequest 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
RestClientRestClient instance
resource
stringResource URL
request
TRequestRequest 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
RestClientrequest
RestRequestcancellationToken
CancellationToken
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
client
RestClientRestClient instance
request
RestRequestThe request
cancellationToken
CancellationTokenCancellation 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
RestClientRestClient instance
resource
stringResource URL
request
TRequestRequest object, must be serializable to JSON
cancellationToken
CancellationTokenCancellation 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
RestClientRestClient instance
resource
stringResource URL
request
TRequestRequest object, must be serializable to JSON
cancellationToken
CancellationTokenCancellation 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
RestClientRestClient instance
resource
stringResource URL
request
TRequestRequest 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
RestClientRestClient instance
resource
stringResource URL
request
TRequestRequest 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
RestClientresource
stringcancellationToken
CancellationToken
Returns
Type Parameters
T
UseAuthenticator(RestClient, IAuthenticator)
public static RestClient UseAuthenticator(this RestClient client, IAuthenticator authenticator)
Parameters
client
RestClientauthenticator
IAuthenticator
Returns
UseJson(RestClient)
Sets the RestClient to only use JSON
public static RestClient UseJson(this RestClient client)
Parameters
client
RestClientClient 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
RestClientClient 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
RestClientqueryEncoder
Func<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
client
RestClientencoder
Func<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
client
RestClientClient instance to work with
Returns
- RestClient
Reference to the client instance