Solid.Http is a library to simplify http calls in C# for .net standard 2.0. It's designed to be async and fully extendable.
- Solid.Http.Core
- Solid.Http.Json
- Solid.Http
- This package is a shortcut for adding Solid.Http.Core and Solid.Http.Json.
- Solid.Http.NewtonsoftJson
- Solid.Http.Zip
- Solid.Http.Xml
The basic package is Solid.Http which includes Solid.Http.Core and Solid.Http.Json.
> dotnet add package Solid.Http
Adding Solid.Http is quite simple when you just need the fluent interface and JSON.
public void ConfigureServices(IServiceCollection services)
{
services.AddSolidHttp();
}
You can also register event handlers during initialization.
public void ConfigureServices(IServiceCollection services)
{
services
.AddSolidHttp(builder =>
{
builder
.Configure(options =>
{
options.OnClientCreated((services, client) =>
{
// your client created handler here.
});
options.OnRequestCreated((services, request) =>
{
// your request created handler here.
});
options.OnHttpRequest(async (services, httpRequest) =>
{
// your http request handler here.
});
options.OnHttpResponse(async (services, httpResponse) =>
{
// your http response handler here.
});
})
;
})
;
}
More about event handlers in the important interfaces section of the Solid.Http.Core README.
You can inject ISolidHttpClientFactory using dependency injection and then use it to create an ISolidHttpClient. ISolidHttpClient is used to create an ISolidHttpRequest which then has a fully async fluent interface for performing an HTTP request.
[Route("[controller]")]
public class PostsController : Controller
{
private ISolidHttpClient _client;
public PostsController(ISolidHttpClientFactory factory)
{
_client = factory.CreateWithBaseAddress("https://jsonplaceholder.typicode.com");
}
[HttpGet]
public async Task<IActionResult> GetAsync(CancellationToken cancellationToken)
{
var posts = await _client
.GetAsync("posts", cancellationToken)
.AsMany<Post>()
;
return Ok(posts);
}
[HttpPut("{id}")]
public async Task<IActionResult> PutAsync([FromRoute] int id, [FromBody] Post body)
{
await _client
.PutAsync("posts/{id}")
.WithNamedParameter("id", id)
.WithJsonContent(body)
.ExpectSuccess()
;
return Ok(body);
}
class Post
{
public int Id { get; set; }
public int UserId { get; set; }
public string Title { get; set; }
public string Body { get; set; }
}
}