user3231144
user3231144

Reputation: 51

Is json-patch supported in restclient?

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

Answers (2)

Michael Baird
Michael Baird

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

Scott Decker
Scott Decker

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

Related Questions