Pavel F
Pavel F

Reputation: 760

What is the best way to handle response in RestSharp?

I use RestSharp and I want to know what is the best way to handle response. There are ErrorMessage, ErrorException and ResponseStatus in RestResponse but how can I check whether request was successful?

I use this code. Does it look ok?

if (response.ResponseStatus != ResponseStatus.Completed)
{
    throw new ApplicationException(response.ErrorMessage);
}

Upvotes: 7

Views: 10530

Answers (2)

SMKS
SMKS

Reputation: 1061

This will not always catch all of the errors. As Jacob said, a ResponseStatus can have a value of Completed even if it returns a 404 or some other bad status.

Instead, use StatusCode which handles all of the HttpStatus responses.

if (response.StatusCode != System.Net.HttpStatusCode.OK)
  throw new ApplicationException(response.ErrorMessage);

Upvotes: 9

Mostafa Soghandi
Mostafa Soghandi

Reputation: 1594

It's correct. you can handle other response type by convention

Upvotes: -1

Related Questions