我正在使用 RestClient 将 JSON 参数传递到 C# 中的 api。但是我收到了响应
“从 JSON 读取器读取时,名为“InputArguments”的属性发现了意外的“StartObject”节点。预期为“PrimitiveValue”节点”
我在 C# 中使用下面的代码
var client_startRobot = new RestClient("https://xxxx.xxxx.com/odata/Jobs/UiPath.Server.Configuration.OData.StartJobs");
var request_startRobot = new RestRequest(Method.POST) ;
request_startRobot.AddParameter("Authorization", string.Format("Bearer " + result), ParameterType.HttpHeader);
request_startRobot.AddHeader("content-type", "application/json");
string parameter = "{\"startInfo\":{\"ReleaseKey\": \"ds32rd1-6c98-42f542d-23bb8111ac91d\",\"RobotIds\": [1],\"JobsCount\": 0,\"Strategy\": \"Specific\",\"InputArguments\": {\"add_name\": \"xxxxx-xxx-\"}}}";
request_startRobot.AddParameter("application/json; charset=utf-8", parameter, ParameterType.RequestBody);
IRestResponse response_startRobot = client_startRobot.Execute(request_startRobot);
Run Code Online (Sandbox Code Playgroud)
这似乎是一个仔细阅读API文档的问题。假设您尝试按照此处所述调用协调器,我发现这个示例与您的示例非常相似。
{ "startInfo":
{ "ReleaseKey": "5b754c63-5d1a-4c37-bb9b-74b69e4934bf",
"Strategy": "Specific",
"RobotIds": [ 1553 ],
"NoOfRobots": 0,
"Source": "Manual",
"InputArguments": "{\"message\":\"Aloha\"}"
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,InputArguments 值实际上是一个简单的字符串,而不是实际的 JSON(该字符串包含转义的 JSON 字符串)。
您的请求如下所示:
"InputArguments": {"add_name": "xxxxx-xxx-"}
Run Code Online (Sandbox Code Playgroud)
根据给出的示例,它应该如下所示:
"InputArguments": "{\"add_name\": \"xxxxx-xxx-\"}"
Run Code Online (Sandbox Code Playgroud)
看来您必须“双重转义”字符串的这一部分,如下所示:
\"InputArguments\": \"{\\\"add_name\\\": \\\"xxxxx-xxx-\\\"}\"
Run Code Online (Sandbox Code Playgroud)
实际上构建一个强类型请求对象并将序列化留给 REST 客户端可能会使事情更容易阅读。
| 归档时间: |
|
| 查看次数: |
4294 次 |
| 最近记录: |