In order to update data on client side mobile, Xamarin Forms developers in their apps often use REST services.
And for that we are using HttpClient. It works fine but, unfortunately, it isn't flexible enough and takes time for implementation in each request code like this:
So this code has some parts:
If you want to make the process simpler, Flurl can be used! Flurl.Http is a wrapper on HttpClient, which uses pattern builder for preparing the request and parse response. Also Flurl is testable and we can easily write unit tests on Http requests.
Let’s have a look how that code can be rewritten with Flurl:
Looks simpler, isn’t it? As you must have noticed, we construct url, send request and parse result in one instruction.
And now I’d like to show you how we can use it in the most common situations.
For some API we need to implement Authorization. For that it is necessary to add headers to HttpClient in each request:
Moreover, you can add any parameters to your request if needed, like:
Segments can be added in the following way :
One of the benefits is that we can use Flurl as instance for each service with different base url and config. For that IFlurlClientFactory should be registered in container and inject to constructor of service.
We inject IFlurlClientFactory to constructor and get IFlurlClient instance with base url for each service different if need.
Flurl.Http has its own exceptions:
To sum up, Flurl is very useful in mobile app development. It's very flexible, reduces development time and makes it easy to test.
Flurl documentation: https://flurl.dev/In order to update data on client side mobile, Xamarin Forms developers in their apps often use REST services.