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
optionsRestClientOptionsconfigureDefaultHeadersAction<HttpRequestHeaders>
RestClient(HttpClient, RestClientOptions, bool)
public RestClient(HttpClient httpClient, RestClientOptions options, bool disposeHttpClient = false)
Parameters
httpClientHttpClientoptionsRestClientOptionsdisposeHttpClientbool
RestClient(HttpClient, bool)
public RestClient(HttpClient httpClient, bool disposeHttpClient = false)
Parameters
httpClientHttpClientdisposeHttpClientbool
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
handlerHttpMessageHandlerMessage handler instance to use for HttpClient
disposeHandlerboolDispose 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
baseUrlstring
RestClient(Uri)
Sets the BaseUrl property for requests made by this client instance
public RestClient(Uri baseUrl)
Parameters
baseUrlUri
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
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
CookieContainer
public CookieContainer CookieContainer { get; }
Property Value
DefaultParameters
public ParametersCollection DefaultParameters { get; }
Property Value
Options
public RestClientOptions Options { get; }
Property Value
Methods
AddDefaultParameter(Parameter)
Add a parameter to use on every request made with this client instance
public RestClient AddDefaultParameter(Parameter parameter)
Parameters
parameterParameterParameter to add
Returns
BuildUri(RestRequest)
public Uri BuildUri(RestRequest request)
Parameters
requestRestRequest
Returns
- Uri
Dispose()
public void Dispose()
Dispose(bool)
protected virtual void Dispose(bool disposing)
Parameters
disposingbool
DownloadStream(RestRequest, CancellationToken)
A specialized method to download files as streams.
public Stream? DownloadStream(RestRequest request, CancellationToken cancellationToken = default)
Parameters
requestRestRequestPre-configured request instance.
cancellationTokenCancellationTokenThe 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
requestRestRequestPre-configured request instance.
cancellationTokenCancellationToken
Returns
Execute(RestRequest, CancellationToken)
Executes the request synchronously, authenticating if needed
public RestResponse Execute(RestRequest request, CancellationToken cancellationToken = default)
Parameters
requestRestRequestRequest to be executed
cancellationTokenCancellationTokenThe cancellation token
Returns
ExecuteAsync(RestRequest, CancellationToken)
Executes the request asynchronously, authenticating if needed
public Task<RestResponse> ExecuteAsync(RestRequest request, CancellationToken cancellationToken = default)
Parameters
requestRestRequestRequest to be executed
cancellationTokenCancellationTokenCancellation token
Returns
UseDefaultSerializers()
public void UseDefaultSerializers()
UseSerializer(Func<IRestSerializer>)
Replace the default serializer with a custom one
public RestClient UseSerializer(Func<IRestSerializer> serializerFactory)
Parameters
serializerFactoryFunc<IRestSerializer>Function that returns the serializer instance
Returns
UseSerializer<T>()
Replace the default serializer with a custom one
public RestClient UseSerializer<T>() where T : class, IRestSerializer, new()
Returns
Type Parameters
TThe type that implements IRestSerializer