将参数传递给RestSharp GET请求

时间:2018-12-17 21:34:52

标签: c# service drupal-7 restsharp

我正在尝试与Drupal站点进行通信以访问特定节点。我正在使用C#和RestSharp来访问数据。我可以在FireFox RestClient中获得以下工作:

  

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

它返回正确的节点类型。但是,我尝试使用RestSharp编写的代码都无法正常工作。我要么得到所有节点类型,要么根本不得到。我使用的代码:

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);

这将导致返回每个节点。而且,响应uri是:

  

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

任何人都可以解释如何更正参数的传递以仅访问所请求的类型吗?

1 个答案:

答案 0 :(得分:0)

我能够解决这个问题。通过一些调试代码,我注意到即使拉出所有节点,我正在寻找的类型也丢失了。这使我指向了Drupal系统,在那里我发现该节点未“发布”。这意味着对于外部世界,该节点不存在。因此,我无法检索它。一旦我使节点“发布”,此代码就可以工作:

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");
相关问题