SmartHttpClient 1.0.8
dotnet add package SmartHttpClient --version 1.0.8
NuGet\Install-Package SmartHttpClient -Version 1.0.8
<PackageReference Include="SmartHttpClient" Version="1.0.8" />
<PackageVersion Include="SmartHttpClient" Version="1.0.8" />
<PackageReference Include="SmartHttpClient" />
paket add SmartHttpClient --version 1.0.8
#r "nuget: SmartHttpClient, 1.0.8"
#:package SmartHttpClient@1.0.8
#addin nuget:?package=SmartHttpClient&version=1.0.8
#tool nuget:?package=SmartHttpClient&version=1.0.8
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.8 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.8
- Added
AddSmartHttpClientServices()extension method for simplified dependency registration - Updated framework support messaging (.NET 8+ including .NET 10)
- Documentation improvements
Updated Framework Support
SmartHttpClient now officially supports .NET 8 and above, including .NET 10.
- Targets .NET 8 (LTS), .NET 9, and .NET 10 (LTS)
- 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
HttpClientusage 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
SmartHttpClient provides a convenient service registration extension to wire up all required dependencies with a single line.
Add the following services in your Program.cs file:
builder.Services.AddSmartHttpClientServices();
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
HttpClientinstances. - 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 | Versions 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. |
-
net10.0
- Microsoft.Extensions.Http (>= 10.0.1)
-
net8.0
- Microsoft.Extensions.Http (>= 10.0.1)
-
net9.0
- Microsoft.Extensions.Http (>= 10.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.