HttpRestClientJsonExtensions Class

Namespace
Kampute.HttpClient.NewtonsoftJson
Assembly
  • Kampute.HttpClient.NewtonsoftJson.dll

Definition

Provides extension methods for HttpRestClient that register a Newtonsoft.Json formatter and send JSON payloads.
public static class HttpRestClientJsonExtensions
Inheritance
  • object
  • HttpRestClientJsonExtensions

Remarks

UseNewtonsoftJson(this HttpRestClient, JsonSerializerSettings) registers a NewtonsoftJsonFormatter, which lets the client read JSON responses and advertise JSON in the Accept header. The send helpers write their payloads with the registered NewtonsoftJsonFormatter, or with a new one with default settings if none is registered.

Methods

PatchAsJsonAsync(this HttpRestClient, string, object, CancellationToken)

Sends an asynchronous PATCH request with JSON-formatted payload to the specified URI without processing the response body.
public static Task PatchAsJsonAsync(this 
	HttpRestClient client,
	string uri,
	object payload,
	CancellationToken cancellationToken = default)

Parameters

client HttpRestClient
The HttpRestClient instance to be used for sending the request.
uri string
The URI to which the request is sent.
payload object
The object to serialize as the JSON-formatted HTTP request payload.
cancellationToken CancellationToken optional
A token for canceling the request (optional).

Returns

Task
A task that represents the asynchronous operation.

Exceptions

ArgumentNullException
Thrown if client, uri or payload is null.
HttpResponseException
Thrown if the response status code indicates a failure.
HttpRequestException
Thrown if the request fails due to an underlying issue such as network connectivity, DNS failure, or server certificate validation.
OperationCanceledException
Thrown if the operation is canceled via the cancellation token, or if the timeout of the underlying HttpClient elapses.

PatchAsJsonAsync<T>(this HttpRestClient, string, object, CancellationToken)

Sends an asynchronous PATCH request with JSON-formatted payload to the specified URI and returns the response body deserialized as the specified type.
public static Task<T> PatchAsJsonAsync<T>(this 
	HttpRestClient client,
	string uri,
	object payload,
	CancellationToken cancellationToken = default)

Type Parameters

T
The type of the response object.

Parameters

client HttpRestClient
The HttpRestClient instance to be used for sending the request.
uri string
The URI to which the request is sent.
payload object
The object to serialize as the JSON-formatted HTTP request payload.
cancellationToken CancellationToken optional
A token for canceling the request (optional).

Returns

Task<T>
A task that represents the asynchronous operation, with a result of the specified type.

Exceptions

ArgumentNullException
Thrown if client, uri or payload is null.
HttpResponseException
Thrown if the response status code indicates a failure.
HttpRequestException
Thrown if the request fails due to an underlying issue such as network connectivity, DNS failure, or server certificate validation.
HttpContentException
Thrown if the response body is empty or its media type is not supported.
OperationCanceledException
Thrown if the operation is canceled via the cancellation token, or if the timeout of the underlying HttpClient elapses.

PostAsJsonAsync(this HttpRestClient, string, object, CancellationToken)

Sends an asynchronous POST request with JSON-formatted payload to the specified URI without processing the response body.
public static Task PostAsJsonAsync(this 
	HttpRestClient client,
	string uri,
	object payload,
	CancellationToken cancellationToken = default)

Parameters

client HttpRestClient
The HttpRestClient instance to be used for sending the request.
uri string
The URI to which the request is sent.
payload object
The object to serialize as the JSON-formatted HTTP request payload.
cancellationToken CancellationToken optional
A token for canceling the request (optional).

Returns

Task
A task that represents the asynchronous operation.

Exceptions

ArgumentNullException
Thrown if client, uri or payload is null.
HttpResponseException
Thrown if the response status code indicates a failure.
HttpRequestException
Thrown if the request fails due to an underlying issue such as network connectivity, DNS failure, or server certificate validation.
OperationCanceledException
Thrown if the operation is canceled via the cancellation token, or if the timeout of the underlying HttpClient elapses.

PostAsJsonAsync<T>(this HttpRestClient, string, object, CancellationToken)

Sends an asynchronous POST request with JSON-formatted payload to the specified URI.
public static Task<T> PostAsJsonAsync<T>(this 
	HttpRestClient client,
	string uri,
	object payload,
	CancellationToken cancellationToken = default)

Type Parameters

T
The type of the object expected in the response.

Parameters

client HttpRestClient
The HttpRestClient instance to be used for sending the request.
uri string
The URI to which the request is sent.
payload object
The object to serialize as the JSON-formatted HTTP request payload.
cancellationToken CancellationToken optional
A token for canceling the request (optional).

Returns

Task<T>
A task representing the asynchronous operation, returning a deserialized object of type T.

Exceptions

ArgumentNullException
Thrown if client, uri or payload is null.
HttpResponseException
Thrown if the response status code indicates a failure.
HttpRequestException
Thrown if the request fails due to an underlying issue such as network connectivity, DNS failure, or server certificate validation.
HttpContentException
Thrown if the response body is empty or its media type is not supported.
OperationCanceledException
Thrown if the operation is canceled via the cancellation token, or if the timeout of the underlying HttpClient elapses.

PutAsJsonAsync(this HttpRestClient, string, object, CancellationToken)

Sends an asynchronous PUT request with JSON-formatted payload to the specified URI without processing the response body.
public static Task PutAsJsonAsync(this 
	HttpRestClient client,
	string uri,
	object payload,
	CancellationToken cancellationToken = default)

Parameters

client HttpRestClient
The HttpRestClient instance to be used for sending the request.
uri string
The URI to which the request is sent.
payload object
The object to serialize as the JSON-formatted HTTP request payload.
cancellationToken CancellationToken optional
A token for canceling the request (optional).

Returns

Task
A task that represents the asynchronous operation.

Exceptions

ArgumentNullException
Thrown if client, uri or payload is null.
HttpResponseException
Thrown if the response status code indicates a failure.
HttpRequestException
Thrown if the request fails due to an underlying issue such as network connectivity, DNS failure, or server certificate validation.
OperationCanceledException
Thrown if the operation is canceled via the cancellation token, or if the timeout of the underlying HttpClient elapses.

PutAsJsonAsync<T>(this HttpRestClient, string, object, CancellationToken)

Sends an asynchronous PUT request with JSON-formatted payload to the specified URI and returns the response body deserialized as the specified type.
public static Task<T> PutAsJsonAsync<T>(this 
	HttpRestClient client,
	string uri,
	object payload,
	CancellationToken cancellationToken = default)

Type Parameters

T
The type of the response object.

Parameters

client HttpRestClient
The HttpRestClient instance to be used for sending the request.
uri string
The URI to which the request is sent.
payload object
The object to serialize as the JSON-formatted HTTP request payload.
cancellationToken CancellationToken optional
A token for canceling the request (optional).

Returns

Task<T>
A task that represents the asynchronous operation, with a result of the specified type.

Exceptions

ArgumentNullException
Thrown if client, uri or payload is null.
HttpResponseException
Thrown if the response status code indicates a failure.
HttpRequestException
Thrown if the request fails due to an underlying issue such as network connectivity, DNS failure, or server certificate validation.
HttpContentException
Thrown if the response body is empty or its media type is not supported.
OperationCanceledException
Thrown if the operation is canceled via the cancellation token, or if the timeout of the underlying HttpClient elapses.

SendAsJsonAsync(this HttpRestClient, HttpMethod, string, object, CancellationToken)

Sends an asynchronous request with JSON-formatted payload to the specified URI without processing the response body.
public static Task SendAsJsonAsync(this 
	HttpRestClient client,
	HttpMethod method,
	string uri,
	object payload,
	CancellationToken cancellationToken = default)

Parameters

client HttpRestClient
The HttpRestClient instance to be used for sending the request.
method HttpMethod
The HTTP method to use for the request.
uri string
The URI to which the request is sent.
payload object
The object to serialize as the JSON-formatted HTTP request payload.
cancellationToken CancellationToken optional
A token for canceling the request (optional).

Returns

Task
A task representing the asynchronous operation.

Exceptions

ArgumentNullException
Thrown if client, method, uri or payload is null.
HttpResponseException
Thrown if the response status code indicates a failure.
HttpRequestException
Thrown if the request fails due to an underlying issue such as network connectivity, DNS failure, or server certificate validation.
OperationCanceledException
Thrown if the operation is canceled via the cancellation token, or if the timeout of the underlying HttpClient elapses.

SendAsJsonAsync<T>(this HttpRestClient, HttpMethod, string, object, CancellationToken)

Sends an asynchronous request with JSON-formatted payload to the specified URI.
public static Task<T> SendAsJsonAsync<T>(this 
	HttpRestClient client,
	HttpMethod method,
	string uri,
	object payload,
	CancellationToken cancellationToken = default)

Type Parameters

T
The type of the object expected in the response.

Parameters

client HttpRestClient
The HttpRestClient instance to be used for sending the request.
method HttpMethod
The HTTP method to use for the request.
uri string
The URI to which the request is sent.
payload object
The object to serialize as the JSON-formatted HTTP request payload.
cancellationToken CancellationToken optional
A token for canceling the request (optional).

Returns

Task<T>
A task representing the asynchronous operation, returning a deserialized object of type T.

Exceptions

ArgumentNullException
Thrown if client, method, uri or payload is null.
HttpResponseException
Thrown if the response status code indicates a failure.
HttpRequestException
Thrown if the request fails due to an underlying issue such as network connectivity, DNS failure, or server certificate validation.
HttpContentException
Thrown if the response body is empty or its media type is not supported.
OperationCanceledException
Thrown if the operation is canceled via the cancellation token, or if the timeout of the underlying HttpClient elapses.

UseNewtonsoftJson(this HttpRestClient, JsonSerializerSettings)

Registers a NewtonsoftJsonFormatter with the client, or updates the registered one.
public static NewtonsoftJsonFormatter UseNewtonsoftJson(this HttpRestClient client, JsonSerializerSettings settings = null)

Parameters

client HttpRestClient
The HttpRestClient instance to configure.
settings JsonSerializerSettings optional
The JsonSerializerSettings to use for reading responses and writing payloads. If null, default settings are used.

Returns

NewtonsoftJsonFormatter
The registered NewtonsoftJsonFormatter.

Exceptions

ArgumentNullException
Thrown if client is null.

Remarks

If the client already has a NewtonsoftJsonFormatter, this method sets its settings to settings. Otherwise, it adds a new NewtonsoftJsonFormatter with settings to HttpRestClient.ContentFormatters.