11

I have a problem with IRestResponse in the below:

public async Task<CezanneToken> GetAccessToken()
{
    var client = new RestClient(WebConfigurationManager.AppSettings["TokenUrl"]);
    var request = new RestRequest();
    request.Method = Method.Post;
    request.AddHeader("cache-control", "no-cache");
    request.AddHeader("content-type", "application/x-www-form-urlencoded");
    request.AddParameter("application/x-www-form-urlencoded", "grant_type=client_credentials&client_id=" + WebConfigurationManager.AppSettings["ClientId"] + "&client_secret=" + WebConfigurationManager.AppSettings["ClientSecret"] + "", ParameterType.RequestBody);

    IRestResponse response = await client.ExecuteAsync(request);
    string serStatus = ((RestResponseBase)response).Content;
    CezanneToken details = JsonConvert.DeserializeObject<CezanneToken>(serStatus);
    string Token = details.access_token;
    
    return details;
}

IRestResponse throws

The type or namespace name "IRestResponse" could not be found (are you missing a using directive or an assembly reference?) I cannot make it work. IntelliSense suggest to go with RestResponse> instead of IRestResponse.

But when I go with RestResponse I get Bad Request on the response.

The code sample above is "translated" from Visual Basic but it works just fine in VB. I don't know if the problem with the Bad Request comes from using RestResponse but I assume IRestResponse is needed just as in VB.

I've also seen people using IRestResponse but it just doesn't work for me. I have the using RestSharp; but do I need something else as well?

Ivan Gechev
  • 706
  • 3
  • 9
  • 21

4 Answers4

21

Install the older version of the RestSharp NuGet package (<=106.15).

The latest major version of RestSharp has breaking changes, which are described in the documentation.

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

Downrev to 106.15 worked for me.

Shepster68
  • 71
  • 1
  • 2
2

According the RestSharp docs https://restsharp.dev/v107/#recommended-usage

The IRestResponse interface is deprecated. You get an instance of RestResponse or RestResponse in return.

After just updating the nuget package to the latest stable release v.108.03, Execute request works as intended, without the compiler error.

RestResponse response = _client.Execute(request);  

OR

RestResponse response = await client.ExecuteAsync(request);
chri3g91
  • 1,196
  • 14
  • 16
2

As IRestResponse is deprecated, using of RestResponse is suggested. This should fix the error.

RestResponse response = _client.Execute(request);
Tyler2P
  • 2,324
  • 26
  • 22
  • 31