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
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
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
uristring- The URI to which the request is sent.
payloadobject- The object to serialize as the JSON-formatted HTTP request payload.
cancellationTokenCancellationToken optional- A token for canceling the request (optional).
Returns
- Task
- A task that represents the asynchronous operation.
Exceptions
- ArgumentNullException
- Thrown if
client,uriorpayloadisnull. - 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
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
uristring- The URI to which the request is sent.
payloadobject- The object to serialize as the JSON-formatted HTTP request payload.
cancellationTokenCancellationToken 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,uriorpayloadisnull. - 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
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
uristring- The URI to which the request is sent.
payloadobject- The object to serialize as the JSON-formatted HTTP request payload.
cancellationTokenCancellationToken optional- A token for canceling the request (optional).
Returns
- Task
- A task that represents the asynchronous operation.
Exceptions
- ArgumentNullException
- Thrown if
client,uriorpayloadisnull. - 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
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
uristring- The URI to which the request is sent.
payloadobject- The object to serialize as the JSON-formatted HTTP request payload.
cancellationTokenCancellationToken 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,uriorpayloadisnull. - 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
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
uristring- The URI to which the request is sent.
payloadobject- The object to serialize as the JSON-formatted HTTP request payload.
cancellationTokenCancellationToken optional- A token for canceling the request (optional).
Returns
- Task
- A task that represents the asynchronous operation.
Exceptions
- ArgumentNullException
- Thrown if
client,uriorpayloadisnull. - 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
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
uristring- The URI to which the request is sent.
payloadobject- The object to serialize as the JSON-formatted HTTP request payload.
cancellationTokenCancellationToken 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,uriorpayloadisnull. - 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
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
methodHttpMethod- The HTTP method to use for the request.
uristring- The URI to which the request is sent.
payloadobject- The object to serialize as the JSON-formatted HTTP request payload.
cancellationTokenCancellationToken optional- A token for canceling the request (optional).
Returns
- Task
- A task representing the asynchronous operation.
Exceptions
- ArgumentNullException
- Thrown if
client,method,uriorpayloadisnull. - 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
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
methodHttpMethod- The HTTP method to use for the request.
uristring- The URI to which the request is sent.
payloadobject- The object to serialize as the JSON-formatted HTTP request payload.
cancellationTokenCancellationToken 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,uriorpayloadisnull. - 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
clientHttpRestClient- The HttpRestClient instance to configure.
settingsJsonSerializerSettings 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
clientisnull.
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.

Acceptheader. The send helpers write their payloads with the registered NewtonsoftJsonFormatter, or with a new one with default settings if none is registered.