Table of Contents

Class RestClient

Namespace
RestSharp
Assembly
RestSharp.dll

Client to translate RestRequests into Http requests and process response result

public class RestClient : IDisposable
Inheritance
RestClient
Implements
Inherited Members
Extension Methods

Constructors

RestClient()

Creates an instance of RestClient using the default RestClientOptions

public RestClient()

RestClient(RestClientOptions, Action<HttpRequestHeaders>?)

public RestClient(RestClientOptions options, Action<HttpRequestHeaders>? configureDefaultHeaders = null)

Parameters

options RestClientOptions
configureDefaultHeaders Action<HttpRequestHeaders>

RestClient(HttpClient, RestClientOptions, bool)

public RestClient(HttpClient httpClient, RestClientOptions options, bool disposeHttpClient = false)

Parameters

httpClient HttpClient
options RestClientOptions
disposeHttpClient bool

RestClient(HttpClient, bool)

public RestClient(HttpClient httpClient, bool disposeHttpClient = false)

Parameters

httpClient HttpClient
disposeHttpClient bool

RestClient(HttpMessageHandler, bool)

Creates a new instance of RestSharp using the message handler provided. By default, HttpClient disposes the provided handler when the client itself is disposed. If you want to keep the handler not disposed, set disposeHandler argument to false.

public RestClient(HttpMessageHandler handler, bool disposeHandler = true)

Parameters

handler HttpMessageHandler

Message handler instance to use for HttpClient

disposeHandler bool

Dispose the handler when disposing RestClient, true by default

RestClient(string)

Sets the BaseUrl property for requests made by this client instance

public RestClient(string baseUrl)

Parameters

baseUrl string

RestClient(Uri)

Sets the BaseUrl property for requests made by this client instance

public RestClient(Uri baseUrl)

Parameters

baseUrl Uri

Properties

AcceptedContentTypes

Content types that will be sent in the Accept header. The list is populated from the known serializers. If you need to send something else by default, set this property to a different value.

public string[] AcceptedContentTypes { get; set; }

Property Value

string[]

Authenticator

Authenticator that will be used to populate request with necessary authentication data

public IAuthenticator? Authenticator { get; set; }

Property Value

IAuthenticator

CalculateResponseStatus

Function to calculate the response status. By default, the status will be Completed if it was successful, or NotFound.

public CalculateResponseStatus CalculateResponseStatus { get; set; }

Property Value

CalculateResponseStatus

CookieContainer

public CookieContainer CookieContainer { get; }

Property Value

CookieContainer

DefaultParameters

public ParametersCollection DefaultParameters { get; }

Property Value

ParametersCollection

Options

public RestClientOptions Options { get; }

Property Value

RestClientOptions

Methods

AddDefaultParameter(Parameter)

Add a parameter to use on every request made with this client instance

public RestClient AddDefaultParameter(Parameter parameter)

Parameters

parameter Parameter

Parameter to add

Returns

RestClient

BuildUri(RestRequest)

public Uri BuildUri(RestRequest request)

Parameters

request RestRequest

Returns

Uri

Dispose()

public void Dispose()

Dispose(bool)

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

DownloadStream(RestRequest, CancellationToken)

A specialized method to download files as streams.

public Stream? DownloadStream(RestRequest request, CancellationToken cancellationToken = default)

Parameters

request RestRequest

Pre-configured request instance.

cancellationToken CancellationToken

The cancellation token

Returns

Stream

The downloaded stream.

DownloadStreamAsync(RestRequest, CancellationToken)

A specialized method to download files as streams.

public Task<Stream?> DownloadStreamAsync(RestRequest request, CancellationToken cancellationToken = default)

Parameters

request RestRequest

Pre-configured request instance.

cancellationToken CancellationToken

Returns

Task<Stream>

The downloaded stream.

Execute(RestRequest, CancellationToken)

Executes the request synchronously, authenticating if needed

public RestResponse Execute(RestRequest request, CancellationToken cancellationToken = default)

Parameters

request RestRequest

Request to be executed

cancellationToken CancellationToken

The cancellation token

Returns

RestResponse

ExecuteAsync(RestRequest, CancellationToken)

Executes the request asynchronously, authenticating if needed

public Task<RestResponse> ExecuteAsync(RestRequest request, CancellationToken cancellationToken = default)

Parameters

request RestRequest

Request to be executed

cancellationToken CancellationToken

Cancellation token

Returns

Task<RestResponse>

UseDefaultSerializers()

public void UseDefaultSerializers()

UseSerializer(Func<IRestSerializer>)

Replace the default serializer with a custom one

public RestClient UseSerializer(Func<IRestSerializer> serializerFactory)

Parameters

serializerFactory Func<IRestSerializer>

Function that returns the serializer instance

Returns

RestClient

UseSerializer<T>()

Replace the default serializer with a custom one

public RestClient UseSerializer<T>() where T : class, IRestSerializer, new()

Returns

RestClient

Type Parameters

T

The type that implements IRestSerializer