首页 > 解决方案 > 将参数传递给 RestSharp GET 请求

问题描述

我正在尝试与 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 }

谁能解释如何更正参数的传递以仅访问请求的类型?

标签: c#servicedrupal-7restsharp

解决方案


我能够解决这个问题。通过一些调试编码,我注意到即使我拉出所有节点,我正在寻找的类型也丢失了。这将我指向 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");

推荐阅读