4

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?

Community
  • 1
  • 1
user3231144
  • 51
  • 1
  • 4
  • Have you tried using parameters instead of AddBody? – Prix Jan 24 '14 at 09:11
  • It seems I have overcome this problem but the question is still valid because my error description has changed now. The error description is as following:{"type":"error","status":415,"code":"unsupported_media_type","help_url":"http:\/\/\/#errors","message":"Content-Type must be application\/json-patch+json","request_id":"1953d8ac6"} I get unsupported_media_type error and error is raised in the restclient.execute() method. Is there a way to solve this problem? – user3231144 Jan 24 '14 at 21:24
  • Prix, Yes I had tried parameters and that had not worked but anyway I am able to pass the operations in the body now but the issue is related to json-patch. It seems execute() method does not support json-patch response. Any ideas? – user3231144 Jan 24 '14 at 21:28

2 Answers2

3

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);
Michael Baird
  • 1,329
  • 12
  • 19
1

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);
Scott Decker
  • 4,229
  • 7
  • 24
  • 39