【问题标题】:Grapevine RestClient Json BodyGrapevine RestClient Json 正文
【发布时间】:2017-08-20 08:00:16
【问题描述】:

我很难通过发送 json 内容来组合请求,在 4.x 文档中找不到任何内容,它与版本 3.x 完全不同

RestClient client = new RestClient();
client.Host = "localhost";
client.Port = 8080;

RestRequest request = new Grapevine.Client.RestRequest("/route1");
request.HttpMethod = HttpMethod.POST; 

RestResponse response = client.Execute(request) as RestResponse;

【问题讨论】:

  • 我在这里看不到任何地方可以向有效负载添加任何内容(将内容放入请求的正文中)。你错过了一步吗?

标签: grapevine


【解决方案1】:

在您的代码中的某处 - 在发送您的请求之前 - 您需要设置请求的正文(或有效负载)。

request.Payload = "send this data";

有效负载只是一个字符串,因此您可以在进行分配之前将对象序列化为 JSON 字符串(并适当地设置 ContentType 属性)。 Json.NET 库被广泛用于完成此任务。您也可以手动执行此操作:

request.ContentType = ContentType.JSON;
request.Payload = "{\"key\":\"value\"}";

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-11-16
  • 2017-12-30
  • 2016-12-17
  • 1970-01-01
  • 1970-01-01
  • 2018-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多