Search code examples
c#servicedrupal-7restsharp

Passing parameter to RestSharp GET request


I'm trying to communicate with a Drupal site to access a particular node. I'm using C# and RestSharp to gain access to the data. I can get the following to work in FireFox RestClient:

http://localhost/drupal/gpa/node?parameters[type]=product_activation

It returns the correct node type. But, everything I've tried to code using RestSharp does not work. I either get all node types, or, none at all. The code I use:

restClient = new RestClient(“http://localhost/drupal/gpa/node”);
RestRequest request = new RestRequest();

request.Method = Method.GET;
request.AddHeader("Content-Type", "application/json");
request.AddHeader("X-CSRF-Token", csrftoken);
request.AddHeader("Cookie", sessid);
request.AddParameter("type", "product_activation", ParameterType.GetOrPost);

var response = restClient.Execute(request);

This results in every node being returned. And, the response uri is:

{http://localhost/drupal/gpa/node?type=product_activation}

Can anyone explain how to correct the passing of the parameter to access just the requested type?


Solution

  • I was able to resolve this. Through some debug coding, I noticed that even when I pulled all of the nodes, the type I was looking for was missing. That pointed me to the Drupal system where I discovered that the node was not "published". This meant that to the outside world, the node did not exist. Hence, I could not retrieve it. This code worked once I made the node "published":

    restClient = new RestClient(RestGetNodes);
    RestRequest request = new RestRequest();
    
    request.Method = Method.GET;
    request.AddHeader("Content-Type", "application/json");
    request.AddHeader("X-CSRF-Token", csrftoken);
    request.AddHeader("Cookie", sessid);
    request.AddParameter("parameters[type]", "product_activation");