【问题标题】:Rest Client adding json body return MESSAGE_NOT_READABLERest Client 添加 json 正文返回 MESSAGE_NOT_READABLE
【发布时间】:2023-01-13 04:22:47
【问题描述】:

我正在尝试在 C# 中使用 RestClient

var client = new RestClient(signUrl);
var request = new RestRequest(signUrl, Method.Put);

如何将以下 JSON 添加到请求正文中

{
  "device_configuration_general": {
    "id": "fffc16e2-2cf3-40f6-92df-a5b65e617b1c"
  }
}

我试过了

var body = "{\"device_configuration_general\": {\"id\": \"f60c3043-cedc-4d2e-b713-5c5dce842dc4}}"

request.AddBody(body);

RestResponse response = await client.ExecuteAsync(request);

但越来越

"{\"code\":\"MESSAGE_NOT_READABLE\",\"message\":\"JSON parse error: Unexpected end-of-input in VALUE_STRING\",\"serviceName\":\"Device Service\",\"traceId\":\"adf3a952f0585e7d\",\"environment\":\"dev\",\"details\":null}"

我也试过

var body = "{\"device_configuration_general\": {\"id\": \"e8729ce5-e741-4b17-844e-e129133de49e\",}}"

但得到:

"{\"code\":\"MESSAGE_NOT_READABLE\",\"message\":\"JSON parse error: Unexpected character ('}' (code 125)): was expecting double-quote to start field name\",\"serviceName\":\"Device Service\",\"traceId\":\"55804813c2f97aae\",\"environment\":\"dev\",\"details\":null}"

【问题讨论】:

    标签: c# rest-client


    【解决方案1】:

    您可以将 JSON 正文添加到请求正文中:

    request.Parameters.Add(new Parameter() { 
        ContentType = "application/json",
        Type = ParameterType.RequestBody, 
        Value = body
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-25
      • 1970-01-01
      • 2016-12-27
      • 2021-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多