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

blac040
  • 127
  • 2
  • 9
  • 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? – Crowcoder Mar 21 '22 at 14:39
  • When i type client, intelligence show RestClient client – blac040 Mar 21 '22 at 14:49
  • okay, So "client." intelligence gives me ExecuteAsync , ExecuteGetAsync etc. But not Execute. – blac040 Mar 21 '22 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. – Crowcoder Mar 21 '22 at 14:54
  • But I don't want to use async nature, then what should I do? – blac040 Mar 21 '22 at 14:55
  • 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. – Crowcoder Mar 21 '22 at 15:02
  • @Crowcoder Okay I will try – blac040 Mar 21 '22 at 15:05

1 Answers1

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.

Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83
  • Thanks. Now I understood. Can you also please check this https://stackoverflow.com/questions/71568629/getting-error-while-creating-job-from-jobs-api – blac040 Mar 22 '22 at 08:09