RS.HttpClientFactoryService A lightweight and flexible HTTP client service for ASP.NET Core, built on top of IHttpClientFactory. Simplifies making HTTP requests with built-in support for: GET, POST, PUT, PATCH, DELETE requests JSON serialization/deserialization with System.Text.Json Optional custom headers, authorization tokens, and named HttpClients Multipart and FormUrlEncoded requests Handling responses with structured HttpResult<T> Optional CancellationToken support for request cancellation Minimal boilerplate code, making HTTP calls easier and more maintainable Ideal for developers who want a consistent, reusable, and testable HTTP client service across projects without relying on heavy third-party libraries. Read more: https://www.theravinder.com/blog/rs-httpclientfactoryservice-colon-and-hyphen-a-cleaner-way-to-call-apis-in--dot-net-10068
$ dotnet add package RS.HttpClientFactoryServiceA reusable, flexible HTTP client service for .NET, built on top of IHttpClientFactory.
Supports JSON, form data, multipart requests, GET/POST/PUT/PATCH/DELETE operations, and optional CancellationToken.
## Features
- Simplified HTTP calls with minimal code
- Named HttpClient support
- Automatic JSON serialization/deserialization
- Form URL-encoded and multipart requests
- Optional headers and authorization tokens
- Handles response errors and returns structured HttpResult<T>
- Supports cancellation via CancellationToken
- Works with .NET 8+
## Installation
Install via NuGet (example package name RS.HttpClientFactoryService):
dotnet add package RS.HttpClientFactoryService
## Usage
### Register in Program.cs / Startup.cs
builder.Services.AddHttpClient();
OR
builder.Services.AddHttpClient("JsonPlaceholder", client =>
{
client.BaseAddress = new Uri("https://jsonplaceholder.typicode.com/");
client.DefaultRequestHeaders.Add("Accept", "application/json");
});
builder.Services.AddScoped<IHttpClientFactoryService, HttpClientFactoryService>();
### Define a model
public class Post
{
public int UserId { get; set; }
public int Id { get; set; }
public string Title { get; set; }
public string Body { get; set; }
}
### Basic GET Example
public class PostsController : ControllerBase
{
private readonly IHttpClientFactoryService _httpService;
public PostsController(IHttpClientFactoryService httpService)
{
_httpService = httpService;
}
[HttpGet("posts")]
public async Task<IActionResult> GetPosts(CancellationToken cancellationToken)
{
var result = await _httpService.GetAsync<List<Post>>(
uri: "posts",
clientName: "JsonPlaceholder",
cancellationToken: cancellationToken
);
return result.IsSuccess ? Ok(result.Data) : BadRequest(result.Message);
}
}
var newPost = new Post { UserId = 1, Title = "Hello", Body = "World" };
var postResult = await _httpService.PostAsync<Post>(
uri: "posts",
requestJsonData: JsonSerializer.Serialize(newPost),
clientName: "JsonPlaceholder",
cancellationToken: cancellationToken
);
You can pass a CancellationToken to cancel requests:
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
var result = await _httpService.GetAsync<List<Post>>(
"posts",
clientName: "JsonPlaceholder",
cancellationToken: cts.Token
);
- **Multipart POST**:
var multipartContent = new MultipartFormDataContent();
multipartContent.Add(new StringContent("John"), "name");
var response = await _httpService.PostMultipartAsync<HttpResult>(
"users/upload",
multipartContent,
clientName: "MyApi",
cancellationToken: cancellationToken
);
- **Form URL-encoded POST**:
var formData = new Dictionary<string, string>
{
{ "username", "john" },
{ "password", "secret" }
};
var result = await _httpService.PostFormAsync<HttpResult>(
"login",
formData,
clientName: "MyApi",
cancellationToken: cancellationToken
);
HttpClientFactoryService?- Reduces boilerplate code for common HTTP operations
- Supports structured responses (HttpResult<T>) with status and messages
- Works seamlessly with DI and IHttpClientFactory
- Optional cancellation support
- Consistent API for GET, POST, PUT, PATCH, DELETE, form, and multipart requests
For detailed explanation, motivation, and examples — see the full blog post:
[RS.HttpClientFactoryService — A Cleaner Way to Call APIs in .NET]
(https://www.theravinder.com/blog/rs-httpclientfactoryservice-colon-and-hyphen-a-cleaner-way-to-call-apis-in--dot-net-10068)
## License
MIT License