6

I'm working with RestSharp.NetCore package and have a need to call the ExecuteAsyncPost method. I'm struggling with the understanding the callback parameter.

    var client = new RestClient("url");
    request.AddParameter("application/json", "{myobject}",  ParameterType.RequestBody);
    client.ExecuteAsyncPost(request,**callback**, "POST");

The callback is of type Action<IRestResponse,RestRequestAsyncHandler>

Would someone please post a small code example showing how to use the callback parameter with an explanation.

Thanks -C

Collin
  • 71
  • 1
  • 4

2 Answers2

12

This worked for me using ExecuteAsync for a Get call. It should hopefully point you in the right direction. Note that the code and credit goes to https://www.learnhowtoprogram.com/net/apis-67c53b46-d070-4d2a-a264-cf23ee1d76d0/apis-with-mvc

public void ApiTest()
    {
        var client = new RestClient("url");
        var request = new RestRequest(Method.GET);
        var response = new RestResponse();
        Task.Run(async () =>
        {
            response = await GetResponseContentAsync(client, request) as RestResponse;
        }).Wait();
        var jsonResponse = JsonConvert.DeserializeObject<JObject>(response.Content);

    }

public static Task<IRestResponse> GetResponseContentAsync(RestClient theClient, RestRequest theRequest)
    {
        var tcs = new TaskCompletionSource<IRestResponse>();
        theClient.ExecuteAsync(theRequest, response => {
            tcs.SetResult(response);
        });
        return tcs.Task;
    }
Grant Castner
  • 852
  • 6
  • 9
2

RestSharp v106 support .NET Standard 2.0 so if your code worked with RestSharp 105 under .NET Framework - it will also work with .NET Core 2.

RestSharp.NetCore package is not from RestSharp team and is not supported by us. It is also not being updated and the owner does not respond on messages, neither the source code of the package is published.

Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83