0

I am new to restSharp and trying to execute an API test using asynchronous way, below code is running without any issue, can someone help me to update the code such a way that it can handle any error?

public void Test()
    { 
        RestClient client = new RestClient("https://reqres.in/");
        RestRequest request = new RestRequest("/api/users?page=2", Method.Get);
        request.AddHeader("Accept", "application/json");
        var results = getAsyncGetData<UserList>(client, request).GetAwaiter().GetResult();
        Assert.AreEqual(results.Data.per_page,6);

    }

    private static async Task<RestResponse<T>> getAsyncGetData<T>(RestClient client,RestRequest request) where T : new()
    {  
       RestResponse<T> response = await client.ExecuteAsync<T>(request);
        return response;}
Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
Andy
  • 23
  • 1
  • 6
  • What are you invoking the Test method from? This will execute successfully in most cases, although it would be highly preferable to make Test async as well. – David L Aug 15 '22 at 22:05
  • Voting to close until there is an actual question, not just _"help me to update the code such a way that it can handle any error"_. – Metro Smurf Aug 16 '22 at 16:37
  • What do you mean by handle any error? Handle exceptions? Handle error response codes? Have you checked out https://restsharp.dev/error-handling.html – jhambright Aug 17 '22 at 14:08

0 Answers0