Spargine is a collection of open-source assemblies and NuGet packages designed for .NET 10, which I have been developing and maintaining since the release of .NET Framework 2. These assemblies are not only a core part of my projects but are also actively deployed in production environments across several companies I collaborate with.
Get Spargine
You can access the source code and NuGet packages here:
- GitHub: Spargine 10
- NuGet: dotNetDaveNuGet
Working with HttpClient is something most .NET developers do regularly, whether they are calling REST APIs, downloading files, sending JSON payloads, or working with streams. While HttpClient is powerful, writing the same boilerplate code over and over for GET, POST, PUT, PATCH, DELETE, stream handling, JSON serialization, deserialization, status-code validation, and exception handling can quickly clutter your projects.
That is where HttpClientExtensions, part of the DotNetTips.Spargine.Extensions namespace, can help.
These extension methods simplify common HTTP operations by making it easier to send requests, deserialize JSON responses, download streams, and ensure successful responses. Several methods also support JsonTypeInfo<T>, which can improve JSON serialization and deserialization performance while also helping with trimming and AOT-compatible scenarios.
Methods
Below are the methods currently available in HttpClientExtensions.
- DeleteAndDeserializeAsync<TResponse>(Uri url, JsonSerializerOptions options, CancellationToken cancellationToken)
Sends a DELETE request to the specified URL and deserializes the JSON response into an instance of TResponse. Uses stream-based deserialization to minimize memory allocations. - DownloadToStreamAsync(Uri url, Stream destination, CancellationToken cancellationToken)
Sends a GET request to the specified URL and copies the response content directly to the provided destination stream. - GetAndDeserializeAsync<T>(Uri url, JsonSerializerOptions options, CancellationToken cancellationToken)
Sends a GET request to the specified URL and deserializes the JSON response into an instance of the specified type. - GetAndDeserializeFromStreamAsync<T>(Uri url, JsonSerializerOptions options, CancellationToken cancellationToken)
Sends a GET request to the specified URL and deserializes the JSON response from a stream into an instance of the specified type. - GetAndDeserializeFromStreamAsync<T>(Uri url, JsonTypeInfo<T> typeInfo, CancellationToken cancellationToken)
Sends a GET request to the specified URL and deserializes the JSON response from a stream into an instance of T using a source-generated JsonTypeInfo< T> for trim-safe, AOT-compatible deserialization. - GetStreamAsync(Uri url, CancellationToken cancellationToken)
Asynchronously retrieves a stream from the specified URL. - HeadersAsync(Uri url, CancellationToken cancellationToken)
Sends a HEAD request to the specified URL and returns the response headers. - PatchAndDeserializeAsync<TRequest, TResponse>(Uri url, TRequest request, JsonSerializerOptions options, CancellationToken cancellationToken)
Sends a PATCH request with a JSON-serialized body to the specified URL and deserializes the JSON response into an instance of TResponse. - PostAndDeserializeAsync<TRequest, TResponse>(Uri url, TRequest request, JsonSerializerOptions options, CancellationToken cancellationToken)
Sends a POST request with a JSON-serialized body to the specified URL and deserializes the JSON response into an instance of TResponse. - PostAndDeserializeAsync<TRequest, TResponse>(Uri url, TRequest request, JsonTypeInfo<TRequest> requestTypeInfo, JsonTypeInfo<TResponse> responseTypeInfo, CancellationToken cancellationToken)
Sends a POST request with a JSON-serialized body to the specified URL and deserializes the JSON response into an instance of TResponse using source-generated JsonTypeInfo<T> for trim-safe, AOT-compatible serialization and deserialization. - PostAndEnsureSuccessAsync<TRequest>(Uri url, TRequest request, JsonTypeInfo<TRequest> requestTypeInfo, CancellationToken cancellationToken)
Sends a POST request with a JSON-serialized body to the specified URL and ensures a successful response, returning the HttpStatusCode. Uses a source-generated JsonTypeInfo<T> for trim-safe, AOT-compatible serialization. - PostAndEnsureSuccessAsync<TRequest>(Uri url, TRequest request, JsonSerializerOptions options, CancellationToken cancellationToken)
Sends a POST request with a JSON-serialized body to the specified URL and ensures a successful response, returning the HttpStatusCode. - PutAndDeserializeAsync<TRequest, TResponse>(Uri url, TRequest request, JsonSerializerOptions options, CancellationToken cancellationToken)
Sends a PUT request with a JSON-serialized body to the specified URL and deserializes the JSON response into an instance of TResponse.
Why These Extensions Matter
The goal of these methods is to make common HTTP operations easier, cleaner, and more consistent across your .NET projects. Instead of repeatedly writing request setup, response validation, stream handling, and serialization logic, you can use these extension methods to keep your code focused on what it is trying to accomplish.
They are especially useful when your application frequently works with JSON APIs, stream-based responses, or performance-sensitive serialization scenarios.
Summary
The next time your code needs to use HttpClient to send or receive JSON, download content, process streams, or validate HTTP responses, consider using the methods in HttpClientExtensions.
These extensions can help reduce repetitive code, improve readability, support better performance, and make your HTTP communication code more consistent across your projects.
For even more help when working with HttpClient, also check out HttpClientHelper in the DotNetTips.Spargine project and NuGet package.
Get Involved!
The success of open-source projects like Spargine relies on community contributions. If you find these updates useful or have ideas for further improvements, I encourage you to contribute by:
- Submitting pull requests
- Reporting issues
- Suggesting new features
Your input is invaluable in making Spargine an even more powerful tool for the .NET community.
If you are interested in contributing or have any questions, feel free to contact me via email at dotnetdave@live.com. Your support and collaboration are greatly appreciated!
Thank you, and happy coding!
Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
If you liked this article, please buy David a cup of Coffee by going here: https://www.buymeacoffee.com/dotnetdave
© The information in this article is copywritten and cannot be reproduced in any way without express permission from David McCarter.
Discover more from dotNetTips.com
Subscribe to get the latest posts sent to your email.

