A typical http call using RestSharp looks as follows:
var client = new RestClient("http://exampleapi.com");
var request = new RestRequest("someapi", Method.GET);
IRestResponse response = client.Execute(request);
From the documentation at https://github.com/restsharp/RestSharp/wiki/Getting-Started:
If there is a network transport error (network is down, failed DNS lookup, etc), RestResponse.Status will be set to ResponseStatus.Error, otherwise it will be ResponseStatus.Completed. If an API returns a 404, ResponseStatus will still be Completed. If you need access to the HTTP status code returned you will find it at RestResponse.StatusCode.
Further, the following appear to be behaviors of RestSharp responses:
response.ErrorException
will be populated with some Exception-derived type and response.ErrorMessage
will contain some message error string and response.StatusCode
will be set to ResponseStatus.Error
, Response.Status.Aborted
, ResponseStatus.TimedOut
, etc.response.StatusCode
will be set to NotFound
, etc, Response.ErrorException
and Response.Error
will be null
and response.StatusCode
will be set to 'ResponseStatus.Completed`.I may have missed some possible responses, but I think the gist is there.
Given this, how should I determine response success or failure? Options include:
ErrorException == null
then check the http responseresponse.ResponseStatus == ResponseStatus.Completed
then check Response.StatusCode and depending on the result, grab the response data and handle accordingly if not what you expectErrorException
I don't want to overthink this but I am assuming there's a pattern (for lack of better term) for handling this cleanly.
RestSharp is an open source HTTP client library that makes it easy to consume RESTful services. RestSharp provides a developer friendly interface to work with RESTful services while abstracting the internal intricacies of working with HTTP requests. RestSharp supports both synchronous and asynchronous requests.
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.
RestSharp v107 changes the library API surface and its behaviour significantly. We advise looking at v107 docs to understand how to migrate to the latest version of RestSharp. The main purpose of RestSharp is to make synchronous and asynchronous calls to remote resources over HTTP.
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.
Given this, how should I determine response success or failure?
I suggest checking ((int) response.StatusCode)
. If 200 <= ((int) response.StatusCode) && ((int) response.StatusCode) < 400
, it succeeded (for an intentionally vague definition of success). Otherwise, the status code is either outside this range, or response.ErrorException
has something interesting.
If you're expecting a specific status code you may wish to take an action if it's some other, non-error code. For example, if I expect only 200 responses, I might want to log a 301 response as a warning, but continue on.
See this answer for a slightly different approach.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With