HttpRestClientJsonExtensions Class

Namespace
Kampute.HttpClient.Json
Assembly
  • Kampute.HttpClient.Json.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, JsonSerializerOptions)

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, JsonSerializerOptions options = null)

Parameters

client HttpRestClient
The HttpRestClient instance to configure.
options JsonSerializerOptions optional
The JsonSerializerOptions to use for deserializing JSON responses. if null, default options will be used.

Returns

JsonContentDeserializer
The JsonContentDeserializer used for JSON content deserialization.

Remarks

If the client already has a JsonContentDeserializer, this method updates its options with the provided options. Otherwise, it adds a new JsonContentDeserializer with the specified options to the client's response deserializers.

GetJsonSerializerOptions(this HttpRestClient)

Retrieves the options used by the HttpRestClient when serializing payloads as JSON.
public static JsonSerializerOptions GetJsonSerializerOptions(this HttpRestClient client)

Parameters

client HttpRestClient
The HttpRestClient instance to query.

Returns

JsonSerializerOptions
The JsonSerializerOptions 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

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 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, 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

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 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, 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

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 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, 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

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 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, 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

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 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, 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

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 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, 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

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 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, 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

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 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, 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.

SetJsonSerializerOptions(this HttpRestClient, JsonSerializerOptions)

Configures the HttpRestClient to use the specified options when serializing payloads as JSON.
public static void SetJsonSerializerOptions(this HttpRestClient client, JsonSerializerOptions options)

Parameters

client HttpRestClient
The HttpRestClient instance to configure.
options JsonSerializerOptions
The JsonSerializerOptions to use for serializing payload as JSON. if null, default options will be used.