Reputation: 51
I am having trouble in using POST method and JSON-Patch operations (Please refer to RFC: https://www.rfc-editor.org/rfc/rfc6902) in RestSharp's RestClient. AddBody() contains something like this:
request.AddBody(new { op = "add", path = "/Resident", value = "32432" });
It errors out. I don't know how to pass json-patch
operations in the body. I have tried everything I could. Is there a solution for this problem?
Upvotes: 4
Views: 3855
Reputation: 1329
This is an improved version of Scott's answer. I did not like querying the parameters and RestSharp provides a way to set the name directly with 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);
Upvotes: 3
Reputation: 4297
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);
Upvotes: 1