16

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:

  • RestClient.Execute() will never throw an exception
  • If the network request fails, ie a condition occurs that would normally result in an exception (eg network timed out, unreachable, name could not be resolved), then 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.
  • If the network request succeeds but there's some HTTP error (eg 404 not found, 500 server error, etc.), then 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:

  • If ErrorException == null then check the http response
  • If response.ResponseStatus == ResponseStatus.Completed then check Response.StatusCode and depending on the result, grab the response data and handle accordingly if not what you expect
  • If the http response is some error then depending on the type of error check ErrorException
  • More...?

I don't want to overthink this but I am assuming there's a pattern (for lack of better term) for handling this cleanly.

Robert
  • 353
  • 3
  • 14

3 Answers3

4

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.

LexH
  • 1,047
  • 9
  • 21
1

I think response code is type of HttpStatusCode. so you can get the code like below. After that you know how to handle it I guess.

RestResponse response = client.Execute(request);
HttpStatusCode statusCode = response.StatusCode;
int numericStatusCode = (int)statusCode;
Seminda
  • 1,745
  • 12
  • 15
0

I am using !response.IsSuccessful as a good starting point after making sure the response is not null.

Jeff Blumenthal
  • 442
  • 6
  • 8