Is json-patch supported in restclient?

I am having problems using the POST method and JSON-Patch (see RFC: http://tools.ietf.org/html/rfc6902 ) in RestSharp RestClient. AddBody () contains something like this:

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

Mistakes I do not know how to transfer json-patchoperations in the body. I tried everything I could. Is there a solution to this problem?

+4
source share
2 answers

This is an improved version of Scott's answer. I did not like to request parameters, and RestSharp provides a way to directly set the name using 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);
+2
source

This works for me:

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);
+1

Source: https://habr.com/ru/post/1523478/


All Articles