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
RestClientOptionsconfigureDefaultHeaders
Action<HttpRequestHeaders>
RestClient(HttpClient, RestClientOptions, bool)
public RestClient(HttpClient httpClient, RestClientOptions options, bool disposeHttpClient = false)
Parameters
httpClient
HttpClientoptions
RestClientOptionsdisposeHttpClient
bool
RestClient(HttpClient, bool)
public RestClient(HttpClient httpClient, bool disposeHttpClient = false)
Parameters
httpClient
HttpClientdisposeHttpClient
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
HttpMessageHandlerMessage handler instance to use for HttpClient
disposeHandler
boolDispose 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
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
parameter
ParameterParameter to add
Returns
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
RestRequestPre-configured request instance.
cancellationToken
CancellationTokenThe 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
RestRequestPre-configured request instance.
cancellationToken
CancellationToken
Returns
Execute(RestRequest, CancellationToken)
Executes the request synchronously, authenticating if needed
public RestResponse Execute(RestRequest request, CancellationToken cancellationToken = default)
Parameters
request
RestRequestRequest to be executed
cancellationToken
CancellationTokenThe cancellation token
Returns
ExecuteAsync(RestRequest, CancellationToken)
Executes the request asynchronously, authenticating if needed
public Task<RestResponse> ExecuteAsync(RestRequest request, CancellationToken cancellationToken = default)
Parameters
request
RestRequestRequest to be executed
cancellationToken
CancellationTokenCancellation token
Returns
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
UseSerializer<T>()
Replace the default serializer with a custom one
public RestClient UseSerializer<T>() where T : class, IRestSerializer, new()
Returns
Type Parameters
T
The type that implements IRestSerializer