HttpRestClientJsonExtensions Class
- Namespace
- Kampute.HttpClient.NewtonsoftJson
- Assembly
- Kampute.HttpClient.NewtonsoftJson.dll
Definition
Provides extension methods for HttpRestClient to support JSON-based HTTP operations.
public static class HttpRestClientJsonExtensions- Inheritance
- object
- HttpRestClientJsonExtensions
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(this 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
clientHttpRestClient- The HttpRestClient instance to configure.
settingsJsonSerializerSettings optional- The 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(this HttpRestClient)
Retrieves the settings used by the HttpRestClient when serializing payloads as JSON.
public static JsonSerializerSettings GetJsonSerializerSettings(this HttpRestClient client)Parameters
clientHttpRestClient- The HttpRestClient instance to query.
Returns
- JsonSerializerSettings
- The JsonSerializerSettings if set; otherwise,
null.
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
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, 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(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
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, 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>(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
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, 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(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
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, 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>(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
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, 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(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
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, 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>(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
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, 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(this 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
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
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, 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(this 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
clientHttpRestClient- The HttpRestClient instance to configure.
settingsJsonSerializerSettings- The JsonSerializerSettings to use for serializing payload as JSON. if
null, default settings will be used.
