EssentialLayers.Request is a complement to the package `EssentialLayers` to provide an extra layer for using http requests in an easy way.
License
—
Deps
3
Install Size
—
Vulns
✓ 0
Published
Oct 31, 2025
$ dotnet add package EssentialLayers.RequestIs a complement to the package EssentialLayers to provide an extra layer for using http requests in an easy way.
Add the dependencies in your Program.cs file
builder.Services.UseRequest();
And then set the options (Optional)
app.Services.ConfigureRequest(
new HttpOption
{
BaseUri = "https/api.dev",
AppName = "MyApi",
AppVersion = "v1",
InsensitiveMapping = true
}
);
If you want manage multiple instances of the same service, you can use the following code:
yourService.SetOptions(
new HttpOption
{
BaseUri = YOUR_BASE_URI,
Token = YOUR_TOKEN
}
);
IHttpFactory is the new provider to send request toward multiple apis.
To start to use, add the next section in your appsettings.json
"HttpClients": {
"AuthApi": {
"BaseUrl": "https://localhost:5000/api/",
"UserAgent": "FirstApiClient/1.0", (Optional) => Default 'MyApp/1.0'
"ContentType": "application/json" (Optional) => Default 'application/json'
}
"SecondApiClient": {
"BaseUrl": "https://localhost:5001/api/",
"UserAgent": "SecondApiClient/1.0", (Optional) => Default 'MyApp/1.0'
"ContentType": "application/json" (Optional) => Default 'application/json'
}
}
In your Program.cs file
builder.Services.AddHttpClients(builder.Configuration);
builder.Services.ConfigureFactory();
OR
In your Program.cs file
services.AddTransient<AuthHeaderHandler>();
services.AddHttpClient(
clientName, (client) =>
{
client.BaseAddress = new Uri("https://localhost:5000/api/");
client.DefaultRequestHeaders.UserAgent.ParseAdd("AuthApi/1.0");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
).AddHttpMessageHandler<AuthHeaderHandler>();
services.AddHttpClient(
clientName, (client) =>
{
client.BaseAddress = new Uri("https://localhost:5001/api/");
client.DefaultRequestHeaders.UserAgent.ParseAdd("SecondApiClient/1.0");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
).AddHttpMessageHandler<AuthHeaderHandler>();
builder.Services.ConfigureFactory();
And in your specific service.cs file
public class AuthService (IHttpFactory httpFactory)
{
private const string CLIENT_NAME = "AuthApi";
public Task<HttpResponse<LoginResponseDto>> LoginAsync(string userName, string password)
{
return httpFactory.PostAsync<LoginResponseDto, LoginRequestDto>(
CLIENT_NAME, "User/Login", new LoginRequestDto(userName, password)
);
}
}
public record LoginRequestDto(string UserName, string Password);
public record LoginResponseDto(UserDto? User, string Token);
public record UserDto(string Id, string Email, string Name, string PhoneNumber);
31-10-202524-10-202523-10-202522-10-202522-10-202522-10-202521-10-202521-10-202514-10-202509/10/202527/08/202517-07-202521-05-202516-04-202514-04-202514/04/202505-03-202521-02-202518-02-202523-01-202513/12/202412/12/202406/12/202412/11/202405/11/202429/10/2024Created by Mario Soto Moreno