Boost Your .NET Projects with Spargine: Simplify HTTP Request Handling with HttpRequestExtensions

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:

Writing the same code repeatedly when processing HTTP requests is time-consuming and can introduce inconsistencies and maintenance issues. The HttpRequestExtensions class in DotNetTips.Spargine.Extensions provides reusable extension methods for common request-handling tasks, including reading request bodies, retrieving headers, extracting bearer tokens, validating content types, and constructing absolute URIs.

These methods include input validation and are supported by unit tests and performance benchmarks, helping you write cleaner, safer, and more maintainable HTTP request code.

Available Methods

The following methods are available in HttpRequestExtensions.

  • AddRequestId()
    Adds a unique request identifier to the HTTP headers.
  • GetAbsoluteUri()
    Builds the absolute URI from the request’s scheme, host, path base, path, and query string.
  • GetBearerToken()
    Extracts the bearer token from the request’s Authorization header.
  • GetHeaderValue(string headerName)
    Retrieves the value of a specific request header by name.
  • GetRawBodyBytesAsync(CancellationToken cancellationToken)
    Asynchronously reads the request body stream and returns its raw contents as a byte array.
  • GetRawBodyStringAsync(Encoding encoding, CancellationToken cancellationToken)
    Asynchronously reads the request body stream and returns its contents as a string using the specified encoding.
  • HasJsonContentType()
    Determines whether the request uses the application/json content type.
  • IsContentType(string contentType)
    Determines whether the request content type matches the specified value using an ordinal, case-insensitive comparison.
  • TryGetBody<T>(out T? value)
    Attempts to deserialize the request body into an instance of T.
  • TryGetBody(out byte[] value)
    Attempts to read the raw request body into a byte array.
  • TryGetBody<T>(JsonTypeInfo<T> typeInfo, out T? value)
    Attempts to deserialize the request body into an instance of T using a source-generated JsonTypeInfo<T>. This overload supports trim-safe and AOT-compatible JSON deserialization.

Summary

The HttpRequestExtensions class simplifies common HTTP request operations while reducing repetitive code throughout your applications. Its methods make it easier to retrieve headers, inspect content types, read request bodies, extract authentication tokens, and deserialize JSON data.

For additional HTTP-related functionality, also explore Spargine’s HttpClientExtensions, HttpContextExtensions, and HttpResponseHeaderExtensions classes.

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

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.

Leave a Reply