我在RestSharp的RestClient中使用POST方法和JSON-Patch操作(请参阅RFC:http://tools.ietf.org/html/rfc6902)时遇到问题。 AddBody()包含这样的内容:
request.AddBody(new { op = "add", path = "/Resident", value = "32432" });
错误了。我不知道如何在体内传递json-patch
个操作。我尽我所能。这个问题有解决方案吗?
答案 0 :(得分:2)
这是斯科特答案的改进版本。我不喜欢查询参数,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);
答案 1 :(得分: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);