StupidHttpClient 1.0.8

Suggested Alternatives

ReusableHttpClient

dotnet add package StupidHttpClient --version 1.0.8
                    
NuGet\Install-Package StupidHttpClient -Version 1.0.8
                    
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="StupidHttpClient" Version="1.0.8" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="StupidHttpClient" Version="1.0.8" />
                    
Directory.Packages.props
<PackageReference Include="StupidHttpClient" />
                    
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 StupidHttpClient --version 1.0.8
                    
#r "nuget: StupidHttpClient, 1.0.8"
                    
#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 StupidHttpClient@1.0.8
                    
#: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=StupidHttpClient&version=1.0.8
                    
Install as a Cake Addin
#tool nuget:?package=StupidHttpClient&version=1.0.8
                    
Install as a Cake Tool

StupidHttpClient NuGet Package

StupidHttpClient 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 StupidHttpClient

Or via NuGet Package Manager:

Install-Package StupidHttpClient

Register StupidHttpClient in your DI container:

services.AddStupidHttpClient(baseAddress:"https://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 StupidHttpRequestException
  • Easily testable using the IStupidHttpClient interface

Interface

public interface IStupidHttpClient
{
    Task<TResult?> GetAsync<TResult>(string relativePath, Dictionary<string, string> queryParams);
    Task<TResult?> GetAsync<TResult>(string relativePath);
    Task<string> PostAsync<TResult>(string relativeRoute, TResult payload);
    Task<TResponse?> PostAsync<TResult, TResponse>(string relativeRoute, TResult payload);
    Task<string> PatchAsync<TResult>(string relativeRoute, TResult payload);
    Task<TResponse?> PatchAsync<TResult, TResponse>(string relativeRoute, TResult payload);
    Task<string> PutAsync<TResult>(string relativeRoute, TResult payload);
    Task<TResponse?> PutAsync<TResult, TResponse>(string relativeRoute, TResult payload);
    Task<string> DeleteAsync(string relativeRoute);
    Task<string> DeleteAsync<TResult>(string relativeRoute, TResult payload);
}

Usage Examples

GET Request

var response = await StupidHttpClient.GetAsync<MyResponseModel>("/api/data");

With query parameters:

var queryParams = new Dictionary<string, string> { { "id", "123" } };
var response = await StupidHttpClient.GetAsync<MyResponseModel>("/api/data", queryParams);

POST Request

var payload = new MyRequestModel { Data = "Sample data" };
var response = await StupidHttpClient.PostAsync<MyRequestModel, MyResponseModel>("/api/data", payload);

PATCH Request

var payload = new MyRequestModel { Data = "Updated data" };
await StupidHttpClient.PatchAsync("/api/data/123", payload);

PUT Request

var payload = new MyRequestModel { Data = "New data" };
await StupidHttpClient.PutAsync("/api/data/123", payload);

DELETE Request

await StupidHttpClient.DeleteAsync("/api/data/123");

Exception Handling

StupidHttpRequestException captures the HTTP status code and response body:

try
{
    var result = await StupidHttpClient.GetAsync<MyResponseModel>("/api/data/123");
}
catch (StupidHttpRequestException ex)
{
    Console.WriteLine(`Error: ${ex.StatusCode} - ${ex.Message}`);
}

Testing

Mock IStupidHttpClient using libraries like NSubstitute for unit testing:

var mockClient = Substitute.For<IStupidHttpClient>();
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
1.0.8 212 9/15/2024 1.0.8 is deprecated because it is no longer maintained.
1.0.7 169 9/16/2024
1.0.6 167 9/16/2024
1.0.5 192 9/15/2024
1.0.2 166 9/15/2024
1.0.1 167 9/15/2024