ExecuteAsyncPost Example in RestSharp.NetCore

血红的双手。 提交于 2019-12-22 05:45:32

问题


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


回答1:


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;
    }



回答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.



来源:https://stackoverflow.com/questions/40436465/executeasyncpost-example-in-restsharp-netcore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!