4

我在 RestSharp 的 RestClient中使用 POST 方法和 JSON-Patch 操作时遇到问题(请参阅 RFC: https ://www.rfc-editor.org/rfc/rfc6902)。AddBody() 包含如下内容:

request.AddBody(new { op = "add", path = "/Resident", value = "32432" });

它出错了。我不知道如何json-patch在体内传递操作。我已经尽我所能。这个问题有解决方案吗?

4

2 回答 2

3

这是斯科特答案的改进版本。我不喜欢查询参数,RestSharp 提供了一种直接使用AddParameter设置名称的方法

var request = new RestRequest(myEndpoint, Method.PATCH);
request.AddHeader("Content-Type", "application/json-patch+json");
request.RequestFormat = DataFormat.Json;
var body = new
{
  op = "add",
  path = "/Resident",
  value = "32432"
}
request.AddParameter("application/json-patch+json", body, ParameterType.RequestBody);

var response = restClient.Execute(request);
于 2016-05-13T13:44:57.693 回答
1

这对我有用:

var request = new RestRequest(myEndpoint, Method.PATCH);
request.AddHeader("Content-Type", "application/json-patch+json");
request.RequestFormat = DataFormat.Json;
request.AddBody(
    new
    {
        op = "add",
        path = "/Resident",
        value = "32432"
});

request.Parameters.FirstOrDefault(p => p.Type == ParameterType.RequestBody).Name = "application/json-patch+json";

var response = restClient.Execute(request);
于 2016-03-07T15:18:48.837 回答