Soenneker.Extensions.HttpResponseMessage 4.0.2458

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Soenneker.Extensions.HttpResponseMessage --version 4.0.2458
                    
NuGet\Install-Package Soenneker.Extensions.HttpResponseMessage -Version 4.0.2458
                    
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.HttpResponseMessage" Version="4.0.2458" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soenneker.Extensions.HttpResponseMessage" Version="4.0.2458" />
                    
Directory.Packages.props
<PackageReference Include="Soenneker.Extensions.HttpResponseMessage" />
                    
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.HttpResponseMessage --version 4.0.2458
                    
#r "nuget: Soenneker.Extensions.HttpResponseMessage, 4.0.2458"
                    
#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.HttpResponseMessage@4.0.2458
                    
#: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.HttpResponseMessage&version=4.0.2458
                    
Install as a Cake Addin
#tool nuget:?package=Soenneker.Extensions.HttpResponseMessage&version=4.0.2458
                    
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.HttpResponseMessage

Response-body conversion, status-aware results, media-type inspection, and bounded diagnostic logging for HttpResponseMessage.

Installation

dotnet add package Soenneker.Extensions.HttpResponseMessage

Require a successful JSON response

using Soenneker.Extensions.HttpResponseMessage;

using HttpResponseMessage response = await httpClient.GetAsync(uri, cancellationToken);

await response.EnsureSuccess(logger, cancellationToken);
OrderDto order = await response.ToStrict<OrderDto>(logger, cancellationToken);

EnsureSuccess() returns immediately for a successful status. For a failure it can log a non-binary, small body at Information, then throws through EnsureSuccessStatusCode(). ToStrict<T>() requires a nonempty JSON body and throws when reading or deserialization fails; it does not check the HTTP status itself.

Both methods consume the response body. Keep the response alive until conversion finishes and dispose it afterward.

Convert without throwing on malformed content

CustomerDto? customer = await response.To<CustomerDto>(logger, cancellationToken);

To<T>() returns default for no content, unreadable content, non-JSON content, and deserialization failures. It does not treat a non-success status as a conversion failure: if that response contains compatible JSON, it can still return T. Read and conversion failures can be logged; cancellation requested through the supplied token still propagates.

Use ToWithString<T>() when the raw response text is needed alongside the attempted conversion:

(CustomerDto? customer, string? raw) =
    await response.ToWithString<CustomerDto>(logger, cancellationToken);

The tuple contains the raw body even when JSON conversion fails. A read failure produces (default, null), while a known no-content response produces (default, "").

For XML, ToFromXml<T>() follows the forgiving pattern and returns default on read or deserialization failure.

Produce an operation result

OperationResult<CustomerDto> result =
    await response.ToResult<CustomerDto>(logger, cancellationToken);

ToResult<T>() is status-aware:

  • A successful response with valid JSON becomes a successful result containing T.
  • A non-success response containing ProblemDetailsDto becomes a failed result with those details.
  • Missing, malformed, or unexpected content becomes a failed result with the package's generic error message.
  • Status 204, status 205, or a declared zero-length body becomes an empty result carrying the HTTP status.

Overloads accepting JsonTypeInfo<T> are available for source-generated JSON metadata on To<T>(), ToStrict<T>(), and ToResult<T>().

Read or log response text

string? body = await response.ToStringSafe(logger, cancellationToken);
string bodyOrThrow = await response.ToStringStrict(cancellationToken);
await response.LogResponse(logger, cancellationToken);

ToStringSafe() returns null on read failure and an empty string for recognized binary media types. ToStringStrict() delegates to ReadAsStringAsync() and lets failures propagate. LogResponse() writes a debug preview only when debug logging is enabled, the media type is not recognized as binary, and the declared body size is small enough to buffer.

Conversion warnings and errors can include a preview of response content. Do not enable these logs for secrets, credentials, or personal data unless the destination and retention policy are appropriate.

Inspect response metadata

  • IsNoContent() recognizes status 204, status 205, and a declared Content-Length of zero.
  • IsJson() recognizes application/json and media types ending in +json.
  • IsProblemJson() recognizes application/problem+json.
  • IsXml() recognizes application/xml, text/xml, and media types ending in +xml.
  • LooksBinary() recognizes image, audio, video, and application/octet-stream media types.

These checks use response metadata; they do not inspect or validate the complete body.

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 (2)

Showing the top 2 NuGet packages that depend on Soenneker.Extensions.HttpResponseMessage:

Package Downloads
Soenneker.Extensions.HttpClient

A collection of helpful HttpClient extension methods, like retry and auto (de)serialization

Soenneker.Blazor.Consumers.Base

A wrapper around Soenneker.Blazor.ApiClient supporting auto (de)serialization for requests/responses/ProblemDetails.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.2460 0 8/31/2026
4.0.2459 0 8/31/2026
4.0.2458 0 8/31/2026
4.0.2456 0 8/30/2026
4.0.2453 44 8/30/2026
4.0.2451 55 8/30/2026
4.0.2450 29 8/30/2026
4.0.2449 158 8/30/2026
4.0.2447 36 8/30/2026
4.0.2445 38 8/30/2026
4.0.2444 74 8/30/2026
4.0.2442 61 8/30/2026
4.0.2441 377 8/30/2026
4.0.2438 228 8/29/2026
4.0.2437 40 8/29/2026
4.0.2436 36 8/29/2026
4.0.2435 42 8/29/2026
4.0.2434 747 8/26/2026
4.0.2433 225 8/26/2026
4.0.2432 285 8/26/2026
Loading failed

Update dependency Soenneker.Utils.Json to 4.0.2658 (#3006)