Web Api Not recognizing RestClient framework

Viewed 15771

I am trying to do a test post to a 3rd party web api. The code how to post was provided by the documentation from 3rd party.

'RestClient' is a namespace but is used like a type
The type or namespace name 'RestRequest' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'IRestResponse' could not be found (are you missing a using directive or an assembly reference?)
The name 'ParameterType' does not exist in the current context
The name 'Method' does not exist in the current context

The method I am working with:

public void CreatePanelists (Newtonsoft.Json.Linq.JObject data)
{

    Parameters parameters = JsonConvert.DeserializeObject<Parameters>(data.ToString())
    IList<Panelist> panelistList = parameters.panelists;
    string id = parameters.id;

    var client = new RestClient("https://3rdparty.com/v2/events/5893/atendees");
    var request = new RestRequest(Method.POST);
    request.AddHeader("content-type", "application/json");
    request.AddHeader("authorization", "Bearer vJRMZ92lsOE1sKGVF7_4zCezTjxO2RoFeOVGULSJS2_dRhgU5QfZeKqGaoS0ogPv0WISEuZ1RcNzJDUsv27uGA");
    request.AddParameter("application/json", "{\"attendees\":[{\"name\":\"Mary\",\"email\":\"maryjkdfdsgfshdgf@jdfdkjdglfk.jkfgdj\"},{\"name\":\"Mike\",\"email\":\"dfdsgfsdhf@jkgfdgfkdhgfdjg.fkjgdf\"}]}", ParameterType.RequestBody);
    IRestResponse response = client.Execute(request);
}

I installed RestClient 4.0.0 but that did not remove the errors.

I am not using .Net not .net Core. What am I missing?

First time developing this type of project, please pardon if my technical language is not correct.

Your help is much appreciated.

Thank you, Erasmo

UPDATE - I was able to install RestSharp Version 106.11.4.0, and RestClient Version 2.5.0.6.

The only error that persists is

'RestClient' is a namespace but is used like a type
4 Answers

It seems like you missed to include namespace for the use of RestClient package. use the following namespace at top of the file.

using RestSharp;

Also, you should install nuget package before use this RestClient package. If not installed , then use follow command in Nuget package manager console.

PM> install-package RestSharp

First you need to uninstall the RestClient packages :

Manage -> nuget packages

Then install Restsharp package.

PM -> install-package RestSharp

then use using RestSharp

Using RestSharp.RestClient would resolve the error. You would not need RestClient package.

The RestSharp code you're using is out of date for .Net Full Framework < 5.0.

The new RestSharp syntax looks like this: https://restsharp.dev/intro.html#basic-usage

using RestSharp;
using RestSharp.Authenticators;

var client = new RestClient("https://api.twitter.com/1.1");
client.Authenticator = new HttpBasicAuthenticator("username", "password");

var request = new RestRequest("statuses/home_timeline.json", DataFormat.Json);

var timeline = await client.GetAsync<HomeTimeline>(request, cancellationToken);

I've raised a support request #98090 to PostMan to update the RestSharp Code Generated.

Related