When do you use the RESTful API, when do you recommend using RestSharp, and when is HttpClient?

I have been searching for this answer for some time, but have not been successful.

I always used RestSharp, and I find it pretty neat, but then I realized that there is an HttpClient provided by Microsoft, and at first glance it looks like the same functionality.

(I also heard about ServiceStack, and most people prefer it over other alternatives, but paying so much is not an option right now - and not using an older version).

Is it just a matter of personal sympathy, or are there any pros and cons of using one or the other - or maybe there are specific scenarios in which one of them can fit each other better?

Thanks in advance!

+10
source share
1 answer

This largely depends on personal preferences, as you expected. A pair of points:

  • RestSharp came out years before the HttpClient and was much better in functionality and ease of use than anything Microsoft released at that time.

  • HttpClient has a strictly asynchronous (as in async / await) API; RestSharp supports both synchronous and asynchronous calls. But if you are not stuck on the old platform and / or do not support legacy code, you have no particular reason to make synchronous HTTP calls. (Do not bundle threads waiting for potentially long I / O!)

  • RestSharp covers most of the territory that HttpClient does not do, in particular deserialization of responses.

I used RestSharp for several years before switching to HttpClient (I needed async / await support, and as far as I know, RestSharp did not support it at that time, although now), and in the end I wrote and released my Flurl.Http’s own small library, which extends my URL constructor with some quick HTTP / deserialization methods, which are just a thin shell for HttpClient and Json.NET .

+18
source

All Articles