ReusableHttpClient 2.0.14

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

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 GET requests
  • Generic response deserialization for strongly-typed models
  • Custom exception handling via ReusableHttpRequestException
  • Easily testable using the IReusableHttpClient interface

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 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. 
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
2.0.16 219 6/23/2025
2.0.15 127 6/20/2025
2.0.14 130 11/13/2024
1.0.13 146 9/23/2024