Class HttpRestClientJsonExtensions
- Namespace
- Kampute.HttpClient.NewtonsoftJson
- Assembly
- Kampute.HttpClient.NewtonsoftJson.dll
Provides extension methods for HttpRestClient to support JSON-based HTTP operations.
public static class HttpRestClientJsonExtensions
- Inheritance
-
HttpRestClientJsonExtensions
- Inherited Members
Remarks
This static class enhances HttpRestClient by offering methods specifically designed for handling HTTP requests and responses that involve JSON data. It simplifies the process of sending and receiving JSON content, by abstracting the serialization and deserialization of JSON to and from .NET objects.
Methods
AcceptJson(HttpRestClient, JsonSerializerSettings?)
Configures the HttpRestClient to accept JSON responses by adding or updating a JsonContentDeserializer in its response deserializers collection.
public static JsonContentDeserializer AcceptJson(this HttpRestClient client, JsonSerializerSettings? settings = null)
Parameters
client
HttpRestClientThe HttpRestClient instance to configure.
settings
JsonSerializerSettingsThe Newtonsoft.Json.JsonSerializerSettings to use for deserializing JSON responses. if null, default settings will be used.
Returns
- JsonContentDeserializer
The JsonContentDeserializer used for JSON content deserialization.
Remarks
If the client already has a JsonContentDeserializer, this method updates its settings with the provided settings
.
Otherwise, it adds a new JsonContentDeserializer with the specified settings to the client's response deserializers.
GetJsonSerializerSettings(HttpRestClient)
Retrieves the settings used by the HttpRestClient when serializing payloads as JSON.
public static JsonSerializerSettings? GetJsonSerializerSettings(this HttpRestClient client)
Parameters
client
HttpRestClientThe HttpRestClient instance to query.
Returns
- JsonSerializerSettings
The Newtonsoft.Json.JsonSerializerSettings if set; otherwise, null.
PatchAsJsonAsync(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
HttpRestClientThe HttpRestClient instance to be used for sending the request.
uri
stringThe URI to which the request is sent.
payload
objectThe object to serialize as the JSON-formatted HTTP request payload.
cancellationToken
CancellationTokenA token for canceling the request (optional).
Returns
- Task
A task that represents the asynchronous operation.
Exceptions
- ArgumentNullException
Thrown if
uri
orpayload
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, server certificate validation, or timeout.
- 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.
PatchAsJsonAsync<T>(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)
Parameters
client
HttpRestClientThe HttpRestClient instance to be used for sending the request.
uri
stringThe URI to which the request is sent.
payload
objectThe object to serialize as the JSON-formatted HTTP request payload.
cancellationToken
CancellationTokenA token for canceling the request (optional).
Returns
- Task<T>
A task that represents the asynchronous operation, with a result of the specified type.
Type Parameters
T
The type of the response object.
Exceptions
- ArgumentNullException
Thrown if
uri
orpayload
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, server certificate validation, or timeout.
- 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.
PostAsJsonAsync(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
HttpRestClientThe HttpRestClient instance to be used for sending the request.
uri
stringThe URI to which the request is sent.
payload
objectThe object to serialize as the JSON-formatted HTTP request payload.
cancellationToken
CancellationTokenA token for canceling the request (optional).
Returns
- Task
A task that represents the asynchronous operation.
Exceptions
- ArgumentNullException
Thrown if
uri
orpayload
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, server certificate validation, or timeout.
- 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.
PostAsJsonAsync<T>(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)
Parameters
client
HttpRestClientThe HttpRestClient instance to be used for sending the request.
uri
stringThe URI to which the request is sent.
payload
objectThe object to serialize as the JSON-formatted HTTP request payload.
cancellationToken
CancellationTokenA token for canceling the request (optional).
Returns
- Task<T>
A task representing the asynchronous operation, returning a deserialized object of type
T
.
Type Parameters
T
The type of the object expected in the response.
Exceptions
- ArgumentNullException
Thrown if
uri
orpayload
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, server certificate validation, or timeout.
- 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.
PutAsJsonAsync(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
HttpRestClientThe HttpRestClient instance to be used for sending the request.
uri
stringThe URI to which the request is sent.
payload
objectThe object to serialize as the JSON-formatted HTTP request payload.
cancellationToken
CancellationTokenA token for canceling the request (optional).
Returns
- Task
A task that represents the asynchronous operation.
Exceptions
- ArgumentNullException
Thrown if
uri
orpayload
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, server certificate validation, or timeout.
- 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.
PutAsJsonAsync<T>(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)
Parameters
client
HttpRestClientThe HttpRestClient instance to be used for sending the request.
uri
stringThe URI to which the request is sent.
payload
objectThe object to serialize as the JSON-formatted HTTP request payload.
cancellationToken
CancellationTokenA token for canceling the request (optional).
Returns
- Task<T>
A task that represents the asynchronous operation, with a result of the specified type.
Type Parameters
T
The type of the response object.
Exceptions
- ArgumentNullException
Thrown if
uri
orpayload
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, server certificate validation, or timeout.
- 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.
SendAsJsonAsync(HttpRestClient, HttpMethod, string, object, CancellationToken)
Sends an asynchronous POST 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
HttpRestClientThe HttpRestClient instance to be used for sending the request.
method
HttpMethodThe HTTP method to use for the request.
uri
stringThe URI to which the request is sent.
payload
objectThe object to serialize as the JSON-formatted HTTP request payload.
cancellationToken
CancellationTokenA token for canceling the request (optional).
Returns
- Task
A task representing the asynchronous operation.
Exceptions
- ArgumentNullException
Thrown if
method
,uri
orpayload
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, server certificate validation, or timeout.
- 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.
SendAsJsonAsync<T>(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)
Parameters
client
HttpRestClientThe HttpRestClient instance to be used for sending the request.
method
HttpMethodThe HTTP method to use for the request.
uri
stringThe URI to which the request is sent.
payload
objectThe object to serialize as the JSON-formatted HTTP request payload.
cancellationToken
CancellationTokenA token for canceling the request (optional).
Returns
- Task<T>
A task representing the asynchronous operation, returning a deserialized object of type
T
.
Type Parameters
T
The type of the object expected in the response.
Exceptions
- ArgumentNullException
Thrown if
method
,uri
orpayload
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, server certificate validation, or timeout.
- 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.
SetJsonSerializerSettings(HttpRestClient, JsonSerializerSettings?)
Configures the HttpRestClient to use the specified settings when serializing payloads as JSON.
public static void SetJsonSerializerSettings(this HttpRestClient client, JsonSerializerSettings? settings)
Parameters
client
HttpRestClientThe HttpRestClient instance to configure.
settings
JsonSerializerSettingsThe Newtonsoft.Json.JsonSerializerSettings to use for serializing payload as JSON. if null, default settings will be used.