Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When consuming RESTful APIs, when do you recommend using RestSharp and when HttpClient?

I've been searching for this answer for some time but had no success.

I've always used RestSharp and I find it pretty neat, but then realized that there's the HttpClient provided by Microsoft and it looks to cover same functionality at first glance.

(I've also heard about ServiceStack and most people seem to prefer it over the rest of the alternatives, but paying that much now is not an option - nor using the older version).

Is it just a question of personal liking, or are there really any pros and cons of using one or another - or maybe, there are specific scenarios where one of them may fit better than the other?

Thanks in advance!

like image 780
Pablo CG Avatar asked Nov 18 '14 14:11

Pablo CG


People also ask

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.

What is RestSharp API?

RestSharp is a C# library used to build and send API requests, and interpret the responses. It is used as part of the C#Bot API testing framework to build the requests, send them to the server, and interpret the responses so assertions can be made.


Video Answer


1 Answers

It largely comes down to personal preference as you surmised. A couple points:

  • RestSharp came out years before HttpClient and was far superior in functionality and ease of use than anything Microsoft put out at the time.

  • HttpClient has a strictly asynchronous (as in async/await) API; RestSharp supports both synchronous and asynchronous calls. But unless you're stuck on an older platform and/or supporting legacy code, there's little good reason to make HTTP calls synchronously. (Don't tie up threads waiting on potentially long-running I/O!)

  • RestSharp covers a lot of territory that HttpClient by itself does not, most notably deserialization of responses.

I used RestSharp for several years before switching to HttpClient (I wanted async/await support, and to my knowledge RestSharp didn't support it at the time, though it does now), and eventually I wrote and released my own little library, Flurl.Http, which extends my URL builder with some fluent HTTP/deserialization methods that are little more than thin wrappers around HttpClient and Json.NET.

like image 143
Todd Menier Avatar answered Sep 26 '22 05:09

Todd Menier