SmartHttpClient 1.0.7

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

SmartHttpClient

SmartHttpClient is a powerful library for .NET that simplifies working with HttpClient by abstracting away repetitive tasks such as managing client instances, handling JSON serialization/deserialization, configuring headers, authentication, timeouts, and endpoint parameters.

Version 1.0.7 targets **.NET 8 and above, including **.NET 10, and continues to refine API clarity, error handling, and flexibility, giving developers the choice between strongly-typed responses and full access to raw HTTP responses.

With SmartHttpClient, developers no longer need to manually handle JSON (it does it automatically for you). The library is flexible and supports both strongly typed (T) and non-typed requests.

This version has been upgraded to .NET 10 and includes improvements such as:

  • Support for raw HttpResponseMessage via generic detection
  • Improved SendAsync<T>() handling
  • A fixed JSON deserialization bug
  • New non-generic SendAsync() overload returning HttpResponseMessage
  • Cleaner and more reliable internal pipeline

What’s New in v1.0.7

This release focuses on clarity, correctness, and flexibility.

Updated Framework Support

SmartHttpClient now officially supports .NET 8 and above, including .NET 10.

  • Targets .NET 8 (LTS), .NET 9, and .NET 10
  • Fully compatible with the latest .NET runtime and tooling
  • No breaking API changes introduced

This ensures SmartHttpClient remains future-proof while providing a stable foundation for modern .NET applications.

Explicit Raw HTTP Access

A new method has been introduced to allow consumers to retrieve the raw HttpResponseMessage without any automatic handling:

Task<HttpResponseMessage> SendAsyncRaw(HTTPClientRequest request);;

Supported Frameworks

SmartHttpClient targets:

  • .NET 8 (LTS)
  • .NET 9
  • .Net 10 (LTS)

Features

  • Simplifies HttpClient usage with a clean, testable API
  • Automatically serializes request bodies and deserializes JSON responses
  • Supports both strongly-typed (SendAsync<T>) and raw (SendAsyncRaw) HTTP calls
  • Improved, format-agnostic error parsing for clearer diagnostics
  • Built-in support for authentication (e.g. Bearer tokens)
  • Handles headers, timeouts, and endpoint/query parameters consistently
  • Designed for dependency injection and modern .NET applications

Setup

1. Register Services

Add the following services in your Program.cs file to enable dependency injection for SmartHttpClient:

builder.Services.AddHttpClient();
builder.Services.AddTransient<IHTTPClientWrapper, HTTPClientWrapper>();

Usage Examples

Product Service CRUD Operations

This service demonstrates how to perform CRUD (Create, Read, Update, Delete) operations using an HTTPClientWrapper with a strongly-typed Product class.

Features

2. Strongly-Typed Request (T)

Leverage the generic T parameter to deserialize HTTP responses automatically into strongly-typed objects, such as the Product class.


3. Dependency Injection Example

To use SmartHttpClient, inject IHTTPClientWrapper into your services, controllers, or classes:

public class ProductService(IHTTPClientWrapper hTTPClientWrapper)
{
    private readonly IHTTPClientWrapper hTTPClientWrapper = hTTPClientWrapper; 
}

CRUD Operations

4. Get a Single Product by ID

Retrieve a specific product using its unique ID.
Example Use Case: Fetch detailed information about a specific product.

// Get a single product by ID
public async Task<Product> GetItemAsync(int id)
{
    var request = new HTTPClientRequest
    {
        BaseUri = new Uri("https://api.example.com/Product/{id}"),
        Method = HttpMethod.Get,
        Authenticator = new Authenticator
        {
            AccessToken = "TOKEN-ABC1234",
            Auth = AuthenticationMethod.Bearer
        }
    };

    return await _httpClientWrapper.SendAsync<Product>(request);
}   

5. Get a List of Products

Fetch all available products in the system.
Example Use Case: Display all products in a catalog or inventory list.

    // Get a list of products
    public async Task<IEnumerable<Product>> GetItemsAsync()
    {
        var request = new HTTPClientRequest
        {
            BaseUri = new Uri("https://api.example.com/Product/GetItems"),
            Method = HttpMethod.Get,
            Authenticator = new Authenticator
            {
                AccessToken = "TOKEN-ABC1234",
                Auth = AuthenticationMethod.Bearer
            }
        };

        return await _httpClientWrapper.SendAsync<IEnumerable<Product>>(request);
    }

6. Create a New Product

Add a new product to the system.
Example Use Case: Allow users to add new items to an inventory.

   // Create a new product
    public async Task<Product> CreateItemAsync(Product product)
    {
        var request = new HTTPClientRequest
        {
            BaseUri = new Uri("https://api.example.com/Product"),
            Method = HttpMethod.Post,
            RequestBody = product,
            Authenticator = new Authenticator
            {
                AccessToken = "TOKEN-ABC1234",
                Auth = AuthenticationMethod.Bearer
            }
        };

        return await _httpClientWrapper.SendAsync<Product>(request);
    }

7. Update an Existing Product

Modify details of an existing product by its ID.
Example Use Case: Update the price, name, or description of a product.

   // Update an existing product
    public async Task<Product> UpdateItemAsync(Product product)
    {
        var request = new HTTPClientRequest
        {
            BaseUri = new Uri("https://api.example.com/Product"),
            Method = HttpMethod.Put,
            RequestBody = product,
            Authenticator = new Authenticator
            {
                AccessToken = "TOKEN-ABC1234",
                Auth = AuthenticationMethod.Bearer
            }
        };

        return await _httpClientWrapper.SendAsync<Product>(request);
    }

8. Delete a Product by ID

Remove a product from the system using its unique ID.
Example Use Case: Remove discontinued or obsolete products.

     // Delete a product by ID
    public async Task<bool> DeleteItemAsync(int id)
    {
        var request = new HTTPClientRequest
        {
           BaseUri = new Uri("https://api.example.com/Product/{Id}"),
            Method = HttpMethod.Delete,
            Authenticator = new Authenticator
            {
                AccessToken = "TOKEN-ABC1234",
                Auth = AuthenticationMethod.Bearer
            }
        };

        var response = await _httpClientWrapper.SendAsync(request);
        return response.IsSuccessStatusCode;
    }


9. Downloading a File

To handle file downloads, use the HTTPClientWrapper to make a GET request and save the file content locally.

10. File Response Class

Note: The FileResponse class is a custom class that contains the file content and metadata.

public class FileResponse
{
    public string FileName { get; set; } // Name of the file
    public byte[] Content { get; set; } // File content as a byte array
}

11. Downloading File

Example Use Case: Download a file from a remote server and save it to the local filesystem.

public async Task<FileResponse> DownloadFileAsync(int fileId)
{
    var request = new HTTPClientRequest
    {
        BaseUri = new Uri($"{https://api.example.com/FileDownload}{fileId}"),
        Method = HttpMethod.Get,
        Authenticator = new Authenticator
        {
            AccessToken = _sessionUser.AccessToken,
            Auth = AuthenticationMethod.Bearer
        }
    };

    var response = await _httpClientWrapper.SendAsync<FileResponse>(request);

    // Save file locally
    if (response != null && response.Content != null)
    {
        string filePath = Path.Combine("Downloads", response.FileName);
        await File.WriteAllBytesAsync(filePath, response.Content);
        Console.WriteLine($"File downloaded successfully to: {filePath}");
    }

    return response;
}

12. Get Product with a Request

This section demonstrates how to fetch products by passing additional parameters to the controller, such as filtering options or other product data.

13. ProductRequest Class

The ProductRequest class encapsulates all the parameters required for requesting product data from the API.

public class ProductRequest
{  
    public required string Manufacture { get; init; }  
    public required string Model { get; init; }
    public required string Colour { get; init; }   
    public required decimal Price { get; init; }
    public required bool Imported { get; init; }
}

14. Fetching Products with ProductRequest

Here’s how you can use the ProductRequest class to pass parameters to the controller:

public async Task<IEnumerable<Product>> GetProductAsync(ProductRequest productRequest)
{
    var request = new HTTPClientRequest
    {
        BaseUri = new Uri("https://api.example.com/Product/GetItems"),
        Method = HttpMethod.Get,
        EndpointParams = productRequest, // Pass ProductRequest as query parameters
        Authenticator = new Authenticator
        {
            AccessToken = _sessionUser.AccessToken,
            Auth = AuthenticationMethod.Bearer
        }
    };

    return await _httpClientWrapper.SendAsync<IEnumerable<Product>>(request);
}


Dependencies

  • Microsoft.Extensions.Http (>= 10.0.0)

License

This project is licensed under the MIT License. See the LICENSE file for details.


Why Use SmartHttpClient?

With SmartHttpClient, you no longer need to:

  • Create and manage HttpClient instances.
  • Write repetitive boilerplate for JSON serialization/deserialization.
  • Manually handle authentication and headers.

SmartHttpClient is built to save developers time and effort, making HTTP requests clean, efficient, and easy to manage.


Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.8 62 12/29/2025
1.0.7 64 12/29/2025
1.0.6 71 12/29/2025
1.0.5 397 11/19/2025
1.0.4 153 1/17/2025
1.0.3 137 1/16/2025
1.0.2 135 1/16/2025
1.0.1 161 1/16/2025
1.0.0 134 1/16/2025