0

My API is calling REST API using RestSharp and Code looks like this

    var runRequest = { Contains my JSON}
    var client = new RestClient(".....");
    var request = new RestRequest("....", Method.Post);
    string AuthHeader = "...";

    request.AddParameter("application/json", runRequest, ParameterType.RequestBody);
    request.AddParameter("Authorization", "Bearer " + AuthHeader, ParameterType.HttpHeader);
    var response = client.Execute(request); <---- {Red line showing under client}
    return Ok(response);

Error

enter image description here

Because of that red line, I am not able to run my program. Can somebody please tell what the issue can be ?

Thank you

7
  • Not that I use RestSharp, but I think you are looking for ExecuteAsync. When you type "client. ", what does Visual Studio show you in intellisense as available methods? Commented Mar 21, 2022 at 14:39
  • When i type client, intelligence show RestClient client Commented Mar 21, 2022 at 14:49
  • okay, So "client." intelligence gives me ExecuteAsync , ExecuteGetAsync etc. But not Execute. Commented Mar 21, 2022 at 14:54
  • The error is gone because ExecuteAsync takes a RestRequest argument. Now you have to make sure to handle the async nature correctly, by awaiting it. Commented Mar 21, 2022 at 14:54
  • 1
    You can block on the call by doing client.Execute(request).Result. At best this will be an abuse of async benefits, at worst you risk deadlocking a thread. If it is at all an option, I suggest refactoring for async. Commented Mar 21, 2022 at 15:02

1 Answer 1

1

You are using the latest RestSharp version. All the sync methods were deprecated as RestSharp uses HttpClient under the hood, and HttpClient doesn't have any sync overloads, everything is async.

You can find a list of changed or deprecated members of RestClient in the documentation.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Now I understood. Can you also please check this stackoverflow.com/questions/71568629/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.