What should I use in .NET Core test project - HttpClient or RestSharp?

  • I've been scouring the internet all day for a simple comparison, but have yet to find any up to date information regarding the subject.

    I recently joined a team working on a project that is using RestSharp for all API calls. I have previously implemented an HttpClient via the CreateClient method of the .NET Core IHttpclientFactory.

    I'm looking for objective pros/cons regarding each for calling REST endpoints. Is this simply a matter of preference? Is there any underlying difference in how the HttpClient lifecycle is managed between the RestSharp library and the IHttpClientFactory implementation?

    I have noticed, or at least it appears to me, that I am not able to manage the incoming response on as granular a level, but for the sake of the question being asked, let's just chalk that up to my lack of experience with RestSharp.

  • Some prefer to use HttpClient because it is already built into the framework. So there's no need to add extra bloat to your project.

    RestSharp, like any library, is easier to use because someone already did the hard work and ironed out the problems gotten along the way.

    I have used both, and the one is not better than the another. It just depends on your use-case, situation and environment.

    I had a WebAPI proxy project that needed to call another REST API. I chose RestSharp at first because I was already familiar with it from using it in another project. However, I changed to HttpClient eventually because it uses the same return types as the WebAPI controllers, so it just saved me time from having to convert RestSharp's responses back to a HttpResponseMessage.

    Of course each one has its pros and cons, it just boils down to your use-case.

Suggested Topics