ReusableHttpClient 2.0.14
See the version list below for details.
dotnet add package ReusableHttpClient --version 2.0.14
NuGet\Install-Package ReusableHttpClient -Version 2.0.14
<PackageReference Include="ReusableHttpClient" Version="2.0.14" />
<PackageVersion Include="ReusableHttpClient" Version="2.0.14" />
<PackageReference Include="ReusableHttpClient" />
paket add ReusableHttpClient --version 2.0.14
#r "nuget: ReusableHttpClient, 2.0.14"
#:package ReusableHttpClient@2.0.14
#addin nuget:?package=ReusableHttpClient&version=2.0.14
#tool nuget:?package=ReusableHttpClient&version=2.0.14
ReusableHttpClient NuGet Package
ReusableHttpClient is a reusable HTTP client abstraction that simplifies making HTTP requests (GET, POST, PATCH, PUT, DELETE). It integrates easily with Dependency Injection (DI) in .NET and provides a mockable interface for unit testing.
Installation
Install the package via the .NET CLI:
dotnet add package ReusableHttpClient
Or via NuGet Package Manager:
Install-Package ReusableHttpClient
Register ReusableHttpClient in your DI container:
services.AddReusableHttpClient("https://api.restful-api.dev");
Features
- Supports common HTTP methods:
GET,POST,PATCH,PUT,DELETE - Handles query parameters in
GETrequests - Generic response deserialization for strongly-typed models
- Custom exception handling via
ReusableHttpRequestException - Easily testable using the
IReusableHttpClientinterface
Interface
public interface IReusableHttpClient
{
Task<TResult?> GetAsync<TResult>(string relativePath);
Task<string> PostAsync<TResult>(string relativePath, TResult payload);
Task<TResponse?> PostAsync<TResult, TResponse>(string relativePath, TResult payload);
Task<string> PatchAsync<TResult>(string relativePath, TResult payload);
Task<TResponse?> PatchAsync<TResult, TResponse>(string relativePath, TResult payload);
Task<string> PutAsync<TResult>(string relativePath, TResult payload);
Task<TResponse?> PutAsync<TResult, TResponse>(string relativePath, TResult payload);
Task<string> DeleteAsync(string relativePath);
Task<string> DeleteAsync<TResult>(string relativePath, TResult payload);
void ClearAuthorizationHeader(string scheme);
void SetAuthorizationHeader(string scheme, string value);
}
Usage Examples
GET Request
var response = await ReusableHttpClient.GetAsync<MyResponseModel>("/api/data");
With query parameters:
var queryParams = new Dictionary<string, string> { { "id", "123" } };
var response = await ReusableHttpClient.GetAsync<MyResponseModel>("/api/data", queryParams);
POST Request
var payload = new MyRequestModel { Data = "Sample data" };
var response = await ReusableHttpClient.PostAsync<MyRequestModel, MyResponseModel>("/api/data", payload);
PATCH Request
var payload = new MyRequestModel { Data = "Updated data" };
await ReusableHttpClient.PatchAsync("/api/data/123", payload);
PUT Request
var payload = new MyRequestModel { Data = "New data" };
await ReusableHttpClient.PutAsync("/api/data/123", payload);
DELETE Request
await ReusableHttpClient.DeleteAsync("/api/data/123");
ClearAuthorizationHeader
ClearAuthorizationHeader(JwtBearerDefaults.AuthenticationScheme)
SetAuthorizationHeader
SetAuthorizationHeader(JwtBearerDefaults.AuthenticationScheme , "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c")
Exception Handling
ReusableHttpRequestException captures the HTTP status code and response body:
try
{
var result = await ReusableHttpClient.GetAsync<MyResponseModel>("/api/data/123");
}
catch (ReusableHttpRequestException ex)
{
Console.WriteLine(`Error: ${ex.StatusCode} - ${ex.Message}`);
}
Testing
Mock IReusableHttpClient using libraries like NSubstitute for unit testing:
var mockClient = Substitute.For<IReusableHttpClient>();
mockClient.GetAsync<MyResponseModel>("/api/data")
.Returns(new MyResponseModel { Data = "Sample data" });
License
Licensed under the MIT License."
| 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 was computed. 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 was computed. 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. |
-
net8.0
- Microsoft.Extensions.Http (>= 8.0.0)
- Microsoft.Extensions.Logging (>= 8.0.0)
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.