Best way to handle response in RestSharp

What is the best way to handle response. There are ErrorMessage, ErrorException and ResponseStatus in RestResponse but how can you check whether request was successful?

As 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) {
                // To do - execute next steps only if the response code is ok
                CurrentPage = GetInstance<ApplicationPage>().VerifyStatus();
            }
            else {
                Test.Log(LogStatus.Info, "API Request is not completed. API Response content is " + response.Content);
            }

 

The following code shows the Helper class for API to handle the response with no content.

using System.Net;
using RestSharp;

namespace AutoFrame.Helpers
{
    public static class APIHelpers
    {
        public static bool VerifyStatusCode(IRestResponse response) {
            HttpStatusCode statusCode = response.StatusCode;
            int numericStatusCode = (int)statusCode;
            if (response.StatusCode == System.Net.HttpStatusCode.OK || numericStatusCode == 204) {
                return true;
            }
            else {
                return false;
            }
        }
    }
}

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *