Return Data from Task<IRestResponse>

Mitch McConnell 41 Reputation points
2022-08-20T02:45:06.113+00:00

how can I return my return await response.StatusCode;?

Here is the code....

public async Task<IRestResponse> HitAPI(JsonData jsonSend) {  
    var client = new RestClient("");  
    var request = new RestRequest("", Method.Post):  
    request.AddParameter("application/json; charset=utf-8", jsonSend, ParameterType.RequestBody);  
    request.RequestFormat.= DataFormat.Json;  
      
    client.ExecuteAsync(request, response =>  
    {  
      if (response.StatusCode == System.Net.HttpStatusCode.Ok)  
      {  
        //it work  
      }  
      else  
      {  
        //no work  
      }  
    });  
    return await response.StatusCode;  
      
    }  
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,649 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 61,731 Reputation points
    2022-08-22T20:35:40.89+00:00

    besides all the coding errors, StatusCode is a string, and the methods return an IRestResponse

    as you are using callback rather than await, chenter code hereange the code to:

    public async Task<IRestResponse> HitAPI(JsonData jsonSend) {  
         var client = new RestClient("");  
         var request = new RestRequest("", Method.Post):  
         request.AddParameter("application/json; charset=utf-8", jsonSend, ParameterType.RequestBody);  
         request.RequestFormat.= DataFormat.Json;  
              
         return client.ExecuteAsync<IRestResponse>(request, response =>  
         {  
           if (response.StatusCode == System.Net.HttpStatusCode.Ok)  
           {  
             //it work  
             return new SomeRestResponse  
             {  
                  ....  
             }  
           }  
           else  
           {  
             //no work  
             return new FailedRestResponse  
             {  
                  ....  
             }  
           }  
         });  
    }  
    

    but using await would be cleaner:

        public async Task<IRestResponse> HitAPI(JsonData jsonSend) {  
             var client = new RestClient("");  
             var request = new RestRequest("", Method.Post):  
             request.AddParameter("application/json; charset=utf-8", jsonSend, ParameterType.RequestBody);  
             request.RequestFormat.= DataFormat.Json;  
                  
             var response = await client.ExecuteAsync<IRestResponse>(request);  
             if (response.StatusCode != System.Net.HttpStatusCode.Ok)  
             {   
                 //no work   
                 return new FailedRestResponse  
                 {  
                      ....  
                 }  
            }  
      
            //it work  
            return new SomeRestResponse  
            {  
                      ....  
            }  
      
        }  
    

    where SomeRestResponse and FailedRestResponse are implementations of IRestResponse

    0 comments No comments