Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExecuteAsyncPost Example in RestSharp.NetCore

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

like image 261
Collin Avatar asked Nov 05 '16 09:11

Collin


People also ask

What is RestSharp Netcore?

In this article, we're going to learn about using RestSharp in a . NET project. RestSharp is an open-source HTTP Client library that we can use to consume APIs easily. RestSharp features automatic serialization and deserialization, request and response type identification, and numerous authentication inbuilt patterns.

How can I put my body in my RestSharp?

RestSharp supports sending XML or JSON body as part of the request. To add a body to the request, simply call AddJsonBody or AddXmlBody method of the IRestRequest instance. There is no need to set the Content-Type or add the DataFormat parameter to the request when using those methods, RestSharp will do it for you.

Is RestSharp better than HttpClient?

The main conclusion is that one is not better than the other, and we shouldn't compare them since RestSharp is a wrapper around HttpClient. The decision between using one of the two tools depends on the use case and the situation.

What is the use of RestSharp?

RestSharp is a C# library used to build and send API requests, and interpret the responses. It is used as part of the C#Bot API testing framework to build the requests, send them to the server, and interpret the responses so assertions can be made.


2 Answers

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;
    }
like image 111
Grant Castner Avatar answered Sep 23 '22 05:09

Grant Castner


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.

like image 29
Alexey Zimarev Avatar answered Sep 19 '22 05:09

Alexey Zimarev