Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core - HttpClient vs RestSharp [closed]

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.

like image 238
Josh G. Avatar asked Jan 08 '20 18:01

Josh G.


People also ask

Is RestSharp better than HttpClient?

The main conclusion is that one is not better than the other, and we shouldn't compare them since RestSharp is a wrapper around HttpClient. The decision between using one of the two tools depends on the use case and the situation.

Does RestSharp reuse HttpClient?

RestSharp does not use connection pool as HttpClient and does not leave opened sockets after the use.

Does RestSharp work with .NET core?

NetCore package is not from RestSharp team and is not supported by us.

Why should I use RestSharp?

RestSharp is a comprehensive, open-source HTTP client library that works with all kinds of DotNet technologies. It can be used to build robust applications by making it easy to interface with public APIs and quickly access data without the complexity of dealing with raw HTTP requests.


2 Answers

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.

like image 89
tno2007 Avatar answered Oct 18 '22 23:10

tno2007


I'm using RestSharp for a WindowsForm application because it was easy to implement.

Now I'm using Blazor and the latest updates have pushed me from using a simple WebApi call which looked like this:

data = await Http.PostJsonAsync<LocationEntity>("api/GetLocation", data);

And now to get the same functionality I have to do this.

GridRecords = await Http.PostAsJsonAsync<List<LocationEntity>>("api/GetLocations", datalist)
    .Result.Content.ReadFromJsonAsync<List<LocationEntity>>();

I also have to pass my parameter, which is a simple entity as a List so I have extra close to write. It's simple code but it adds no value.

So yeah, I'm seriously looking at RestSharp or alternative to simplify what should be a simple WebApi call.

I don't know what such a simple things has to be made so complex.

My 2 cents. I'm a little behind and now I need to spend a day updating all my WebApi calls because MS decided to change the signature so much.

If I was using a third party I could update my Blazor version without fear of my API logic needing to change. That is the CON of using one vendor for all your libraries.

like image 5
user3448990 Avatar answered Oct 19 '22 01:10

user3448990