Swiss Army Knife
General utility tools for Scada Minds
Static Public Member Functions | List of all members
HttpClientExtensions Class Reference

Extension methods to simplify common HttpClientExtension such as working with JSON. More...

Static Public Member Functions

static Task< TResponse > GetAsJsonAsync< TResponse > (this HttpClient httpClient, Uri uri, int? maxCharactersToPrint=null)
 GETs the given address, and serializes the resulting JSON object into an object of type T. Throws an error on non-2xx status messages. More...
 
static async Task< TResponse > GetAsJsonAsync< TResponse > (this HttpClient httpClient, string url, int? maxCharactersToPrint=null)
 GETs the given address, and serializes the resulting JSON object into an object of type T. Throws an error on non-2xx status messages. The response is expected to be a JSON body and is serialized to TResponse . More...
 
static Task< TResponse > PostAsJsonAsync< TResponse > (this HttpClient httpClient, Uri uri, object? jsonBody=null, int? maxCharactersToPrint=null)
 POSTS to the uri , with a potentially provided jsonBody . The response is expected to be a JSON body and is serialized to TResponse . More...
 
static async Task< TResponse > PostAsJsonAsync< TResponse > (this HttpClient httpClient, string url, object? jsonBody=null, int? maxCharactersToPrint=null)
 POSTS to the url , with a potentially provided jsonBody . The response is expected to be a JSON body and is serialized to TResponse . More...
 

Detailed Description

Extension methods to simplify common HttpClientExtension such as working with JSON.

Member Function Documentation

◆ GetAsJsonAsync< TResponse >() [1/2]

static async Task<TResponse> GetAsJsonAsync< TResponse > ( this HttpClient  httpClient,
string  url,
int?  maxCharactersToPrint = null 
)
inlinestatic

GETs the given address, and serializes the resulting JSON object into an object of type T. Throws an error on non-2xx status messages. The response is expected to be a JSON body and is serialized to TResponse .

Parameters
httpClientHttpClient to use.
urlThe URL to GET. This url has the same semantics as when using the regular "GetAsync" in regards to respecting BaseAddress.
maxCharactersToPrintIf any errors occurs, how many characters of the response body should be included in the response body? If set to null, records the entire response.
Template Parameters
TResponseThe type to attempt to serialize the JSON response to.
Returns
The response from the server as TResponse .

◆ GetAsJsonAsync< TResponse >() [2/2]

static Task<TResponse> GetAsJsonAsync< TResponse > ( this HttpClient  httpClient,
Uri  uri,
int?  maxCharactersToPrint = null 
)
inlinestatic

GETs the given address, and serializes the resulting JSON object into an object of type T. Throws an error on non-2xx status messages.

Parameters
httpClientHttpClient to use.
uriThe URI to GET. Note that this needs to be a fully qualified URI starting with http(s): Any BaseAddress from the HttpClient is not respected.
maxCharactersToPrintIf any errors occurs, how many characters of the response body should be included in the response body? If set to null, records the entire response.
Template Parameters
TResponseThe type to attempt to serialize the JSON response to.
Returns
The response from the server as TResponse .

◆ PostAsJsonAsync< TResponse >() [1/2]

static async Task<TResponse> PostAsJsonAsync< TResponse > ( this HttpClient  httpClient,
string  url,
object?  jsonBody = null,
int?  maxCharactersToPrint = null 
)
inlinestatic

POSTS to the url , with a potentially provided jsonBody . The response is expected to be a JSON body and is serialized to TResponse .

Parameters
httpClientThe HttpClient to use for the operation.
urlThe URL to POST to. This url has the same semantics as when using the regular "postAsync" in regards to respecting BaseAddress.
jsonBodyThe object to be serialized to JSON and included in the body. If not specified or null, no body will be sent.
maxCharactersToPrintIf an error happen, how many characters of the HTTP response to include in the exception message. If not provided, will record the entire response.
Exceptions
HttpRequestExceptionWill throw HttpRequestException if server responds with non-2xx status code.
JsonExceptionWill throw JsonException if it was unable to parse the response body to TResponse .
Template Parameters
TResponseThe response type to parse the returned JSON to.
Returns
The response type serialized into TResponse .

◆ PostAsJsonAsync< TResponse >() [2/2]

static Task<TResponse> PostAsJsonAsync< TResponse > ( this HttpClient  httpClient,
Uri  uri,
object?  jsonBody = null,
int?  maxCharactersToPrint = null 
)
inlinestatic

POSTS to the uri , with a potentially provided jsonBody . The response is expected to be a JSON body and is serialized to TResponse .

Parameters
httpClientThe HttpClient to use for the operation.
uriThe URI to POST to. Note that this needs to be a fully qualified URI starting with http(s): Any BaseAddress from the HttpClient is not respected.
jsonBodyThe object to be serialized to JSON and included in the body. If not specified or null, no body will be sent.
maxCharactersToPrintIf an error happen, how many characters of the HTTP response to include in the exception message. If not provided, will record the entire response.
Exceptions
HttpRequestExceptionWill throw HttpRequestException if server responds with non-2xx status code.
JsonExceptionWill throw JsonException if it was unable to parse the response body to TResponse .
Template Parameters
TResponseThe response type to parse the returned JSON to.
Returns
The response type serialized into TResponse .

The documentation for this class was generated from the following file: