【问题标题】:Passing parameter to RestSharp GET request将参数传递给 RestSharp GET 请求
【发布时间】:2018-12-17 21:34:52
【问题描述】:

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

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

【问题讨论】:

  • 只是猜测,但您是否尝试过parameters[type] 而不是AddParameter 中的type
  • 是的。当我这样编码时,我什么也得不到。
  • 如果你尝试restClient = new RestClient("http://localhost/drupal/gpa/node?parameters[type]=product_activation");会怎样?
  • 谢谢,我也试过了。它返回一个空数组。

标签: c# service drupal-7 restsharp


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    相关资源
    最近更新 更多