Soenneker.Extensions.HttpClient 4.0.3982

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Soenneker.Extensions.HttpClient --version 4.0.3982
                    
NuGet\Install-Package Soenneker.Extensions.HttpClient -Version 4.0.3982
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Soenneker.Extensions.HttpClient" Version="4.0.3982" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soenneker.Extensions.HttpClient" Version="4.0.3982" />
                    
Directory.Packages.props
<PackageReference Include="Soenneker.Extensions.HttpClient" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Soenneker.Extensions.HttpClient --version 4.0.3982
                    
#r "nuget: Soenneker.Extensions.HttpClient, 4.0.3982"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Soenneker.Extensions.HttpClient@4.0.3982
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Soenneker.Extensions.HttpClient&version=4.0.3982
                    
Install as a Cake Addin
#tool nuget:?package=Soenneker.Extensions.HttpClient&version=4.0.3982
                    
Install as a Cake Tool

alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

alternate text is missing from this package README image Soenneker.Extensions.HttpClient

HTTP send helpers for raw strings, JSON payloads, typed success/error bodies, OperationResult<T>, exception-suppressing calls, and retry with cloned requests.

Installation

dotnet add package Soenneker.Extensions.HttpClient

Use these extensions with a long-lived or factory-managed HttpClient; do not create and dispose a new client for every call.

Read a response body

using Soenneker.Extensions.HttpClient;

string body = await client.SendToString(
    "https://api.example.com/health",
    logger,
    cancellationToken);

SendToString() returns the body for both successful and non-successful HTTP statuses. A non-success status is logged when a logger is supplied, but does not itself throw. Transport, content-read, timeout, and cancellation failures propagate.

Deserialize a successful JSON response

Customer customer = await client.SendToType<Customer>(
    "https://api.example.com/customers/42",
    logger,
    cancellationToken);

SendToType<T>() requires a successful HTTP status and a non-null JSON value of T. Non-success status, transport, cancellation, empty-body, and deserialization failures throw. Overloads accept a URI for GET, an HTTP method plus an optional object body, or a prepared HttpRequestMessage. Object bodies are serialized as HTTP content.

Keep HTTP failures as data

OperationResult<Customer> result = await client.SendToResult<Customer>(
    "https://api.example.com/customers/42",
    logger,
    cancellationToken);

(Customer? success, ApiError? error) = await client.SendWithError<Customer, ApiError>(
    "https://api.example.com/customers/42",
    logger,
    cancellationToken);

SendToResult<T>() converts successful JSON to Value, 204 responses to an empty result, and non-success JSON problem details to Problem. Conversion failures become failed operation results, while request-send failures still propagate.

SendWithError<TSuccess,TError>() chooses the payload type from IsSuccessStatusCode; exactly one tuple side is normally populated. SendWithProblemDetails<TSuccess>() is the same pattern with ProblemDetailsDto as the error type. Transport and conversion failures propagate from the non-Try variants.

Suppress request exceptions deliberately

The Try* methods log failures when a logger is supplied and return a sentinel instead of throwing:

  • TrySend() returns (false, null) for an exception or cancellation. For an HTTP non-success response it returns (false, response); the caller must dispose that response.
  • TrySendToString() returns (false, body) for an HTTP error response and (false, null) for an exception or cancellation.
  • TrySendToType<T>() returns default(T) for HTTP errors, cancellation, request failures, or conversion failures.
  • TrySendWithError() and TrySendWithProblemDetails() return default tuples when sending or conversion fails.
  • TrySendToResult<T>() returns a failed result: cancellation maps to HTTP 408 and other caught failures map to HTTP 500.

Because several Try* methods collapse cancellation and failure into null/default values, use the strict variants when callers must distinguish those outcomes.

Retry

Customer customer = await client.SendToTypeWithRetry<Customer>(
    HttpMethod.Get,
    "https://api.example.com/customers/42",
    numberOfRetries: 2,
    logger: logger,
    baseDelay: TimeSpan.FromSeconds(1),
    cancellationToken: cancellationToken);

Retry variants clone the request for each attempt, allowing buffered request content to be sent again. numberOfRetries counts retries after the initial attempt, so 2 allows up to three sends. Delays use exponential backoff from baseDelay (two seconds by default) plus 0–999 ms of jitter.

Retries occur for HttpRequestException and every non-success HTTP status. That includes non-transient 4xx responses. The helper does not inspect Retry-After, and it does not know whether a POST or other operation is idempotent; choose retry counts only when repeating the request is safe. Cancellation stops strict retry methods. Try*WithRetry catches the exhausted failure—including cancellation—and returns its normal null/default sentinel.

Ownership

URI and method/body overloads create and dispose their own request messages. Prepared HttpRequestMessage overloads leave the original request owned by the caller. Methods that deserialize or read content dispose their HttpResponseMessage internally.

SendWithRetry() and TrySendWithRetry() return a live HttpResponseMessage; the caller must dispose it. Failed responses encountered during retry are disposed before the next attempt.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (11)

Showing the top 5 NuGet packages that depend on Soenneker.Extensions.HttpClient:

Package Downloads
Soenneker.Validators.Email.Disposable.Online

A validation module checking for disposable email addresses via online sources

Soenneker.Cloudflare.Turnstile.Validator

A validation module checking Cloudflare Turnstile tokens

Soenneker.Ipqs.Phone

A utility library for IPQualityScore phone related operations

Soenneker.Utils.NuGet

A utility library for various NuGet related operations

Soenneker.Validators.Yahoo.Exists

A validation module checking for Yahoo account existence

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.3983 0 8/30/2026
4.0.3982 0 8/30/2026
4.0.3981 0 8/30/2026
4.0.3979 74 8/30/2026
4.0.3978 32 8/30/2026
4.0.3976 104 8/30/2026
4.0.3975 27 8/30/2026
4.0.3973 180 8/29/2026
4.0.3972 99 8/29/2026
4.0.3971 524 8/26/2026
4.0.3970 166 8/26/2026
4.0.3969 200 8/26/2026
4.0.3968 180 8/26/2026
4.0.3967 72 8/26/2026
4.0.3966 247 8/26/2026
4.0.3965 283 8/25/2026
4.0.3964 157 8/25/2026
4.0.3963 534 8/22/2026
4.0.3962 325 8/22/2026
4.0.3961 332 8/22/2026
Loading failed

Update dependency Soenneker.Extensions.HttpResponseMessage to 4.0.2444 (#4705)