2

I am new to PATCH|MERGE and want to know how to use it, client-side. I am not sure what to send in the body of the payload in JSON.

Here is a contrived example POCO model in C# for discussion purposes.

public class Person
{
  public Guid Id { get; set; }
  public string FullName { get; set; }
  public int Age { get; set; }
}
Luke Puplett
  • 42,091
  • 47
  • 181
  • 266

1 Answers1

3

If you Google for an answer, you'll see various examples of JSON patch where the JSON payload describes one or more operations, such as this one which replaces/updates a value:

PATCH /people/guid123lalala HTTP/1.1    
Content-Type: application/json-patch

{
  "op": "replace",
  "path": "/FullName",
  "value": "Willy Lopez"
}

Or this one:

PATCH /people/guid123lalala HTTP/1.1    
Content-Type: application/json-patch

[
  {"replace": "/FullName", "value": "Willy Lopez"}
]

(Which I'm not even sure is correct for JSON-patch.)

However, the application/json-patch format is not supported. So as of January 2015, for OData on WebApi 2.2, just send the object with the non-changing properties omitted, like this using normal JSON:

PATCH /people/guid123lalala HTTP/1.1    
Content-Type: application/json

{
  FullName: "Willy Lopez"
}
Luke Puplett
  • 42,091
  • 47
  • 181
  • 266
  • If using jQuery to send the message it is also worth noting that you can't just set your Content-Type header and pass a data object. You have to [stringify the object](http://stackoverflow.com/questions/6587221/send-json-data-with-jquery). – user2880616 Mar 08 '16 at 19:45