Flurl
About Flurl: a modern, fluent, asynchronous, testable, portable, buzzword-laden URL builder and HTTP client library for .NET
is similar to RestSharp in that it is a very simple REST library, but is based entirely on a Fluent API (like ServiceStack Http Utils).
Flurl provides an initital extension method on any string that should represent a url. Then, the request or query can be created accordingly using a Fluent API with extension methods.
Info | Link |
---|---|
License | |
Downloads | |
Latest Version | |
Issues | |
Contributors |
Sample
string apiUrl = "my-rest-api.com";
// Get single task
TaskApiModel? task = await $"{apiUrl}/tasks/123".GetJsonAsync<TaskApiModel?>();
// Get all tasks
List<TaskApiModel> tasks = await $"{apiUrl}/tasks".GetJsonAsync<List<TaskApiModel>>();
// Add new task
TaskApiCreateModel newTask = new TaskApiCreateModel { Title = "Buy flowers" };
TaskApiModel addedTask = await $"{apiUrl}/tasks"
.PostJsonAsync(newTask)
.ReceiveJson<TaskApiModel>();
Pros
- Easy to understand.
- Very intuitive programming style.
- API call with one single line of code.
Cons
- Relatively many allocations necessary and therefore more inefficient in comparison.
- More implementation effort, since a wrapper is always necessary in real applications.
- If it goes beyond simple scenarios, you have to do a lot of programming yourself.
- Offering extension methods on any string is not necessarily the best idea: often not wanted and quite considered anti pattern and falls under developer noise.
Full Sample
See the full sample on GitHub: https://github.com/BenjaminAbt/dotnet.rest-samples
Last modified February 27, 2024: Merge pull request #7 from BenjaminAbt/feature/why-rest (4b3c4d0)