SimpleHttpClient
An easy-to-use .NET wrapper for HttpClient
Install / Use
/learn @Mako88/SimpleHttpClientREADME
SimpleHttpClient
An easy-to-use .NET wrapper for HttpClient. No extension methods, and included interfaces allow for easy unit test mocking, and straightforward properties allows for easier debugging (the response body is available as a string, byte array, and/or a typed object).
Installation
SimpleHttpClient is available on NuGet and can installed through the NuGet Package Manager or by running
nuget install SimpleHttpClient
Basic Usage
SimpleHttpClient is designed to be used with dependency injection in order to avoid pitfalls that come with using an HttpClient:
In Program.cs:
// Register SimpleHttpClient with the ServiceCollection
await Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
services.AddSimpleHttpClient();
})
.Build()
.RunAsync();
Then, in the class that will use the SimpleHttpClient:
public class YourClientClass
{
private readonly SimpleClient client;
// Retrieve an ISimpleHttpClient through dependency injection
public YourClientClass(ISimpleHttpClient client)
{
// Set the host on the retrieved client
client.Host = "https://api.sampleapis.com";
}
public async Task<string> MakeRequest()
{
// Pass the path you want to call into the SimpleRequest constructor
var request = new SimpleRequest("/coffee/hot");
// Call MakeRequest on the client, passing your request, and get your response back
var response = await client.MakeRequest(request);
return response.StringBody;
}
}
You can also call MakeRequest with a type to serialize to that type:
public async Task<SomeResponseObject> MakeRequest()
{
// Pass the path you want to call into the Request constructor
var request = new SimpleRequest("/get");
// Call MakeRequest on the client, passing your request, and get your response back
var response = await client.MakeRequest<SomeResponseObject>(request);
return response.Body;
}
If you're using SimpleHttpClient without dependency injection, you can just create an instance of SimpleClient:
public class YourClientClass
{
private readonly SimpleClient client;
public YourClientClass()
{
// Pass the host you'll be calling into the SimpleClient constructor
client = new SimpleClient("https://api.sampleapis.com");
}
public async Task<string> MakeRequest()
{
// Pass the path you want to call into the SimpleRequest constructor
var request = new SimpleRequest("/coffee/hot");
// Call MakeRequest on the client, passing your request, and get your response back
var response = await client.MakeRequest(request);
return response.StringBody;
}
}
NOTE: Although SimpleClient implements IDisposable, it should NOT be created inside a using block, but instead should be disposed with the class that uses it.
Related Skills
node-connect
344.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
99.2kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
344.4kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
344.4kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
