StupidHttpClient 1.0.8
dotnet add package StupidHttpClient --version 1.0.8
NuGet\Install-Package StupidHttpClient -Version 1.0.8
<PackageReference Include="StupidHttpClient" Version="1.0.8" />
<PackageVersion Include="StupidHttpClient" Version="1.0.8" />
<PackageReference Include="StupidHttpClient" />
paket add StupidHttpClient --version 1.0.8
#r "nuget: StupidHttpClient, 1.0.8"
#:package StupidHttpClient@1.0.8
#addin nuget:?package=StupidHttpClient&version=1.0.8
#tool nuget:?package=StupidHttpClient&version=1.0.8
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
GETrequests - Generic response deserialization for strongly-typed models
- Custom exception handling via
StupidHttpRequestException - Easily testable using the
IStupidHttpClientinterface
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 | 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.