I've been searching around but haven't been able to find an answer, if I missed something please just let me know the resource.
I'm building a (mostly) RESTful webservice and need to allow clients to PATCH resources. I realize PATCH by definition isn't RESTful, but I'm still trying to stick as close to the standard as I can.
I'm unsure how to handle patching of properties that are arrays...for instance, my Parent object has multiple Children. So, if someone patches the parent object with a children parameter should I replace existing children or append to them?
Something like
PATCH parent/:id
{
children: [
{ property: value }
]
}
I could just use POST and DELETE on the children to add and remove them from the parent object, but then I want to be able to PATCH the parent object to update other non-array properties, and it seems wrong to allow patching of just some properties and not others. Maybe this is the right answer, I'm not sure.
I've read a lot of posts about proper PATCHing but none of them seem to talk about this issue. If anyone has any input I would appreciate it-
A Client should use HTTP PATCH request method when they want to partially modify the state of a resource. A prerequisite to the PATCH request is that, the resource must already exist on the server, as the server won't create the resource.
When a client needs to replace an existing Resource entirely, they can use PUT. When they're doing a partial update, they can use HTTP PATCH. For instance, when updating a single field of the Resource, sending the complete Resource representation can be cumbersome and uses a lot of unnecessary bandwidth.
A PATCH request is considered a set of instructions on how to modify a resource. Contrast this with PUT ; which is a complete representation of a resource. A PATCH is not necessarily idempotent, although it can be. Contrast this with PUT ; which is always idempotent.
The PATCH HTTP method is used to modify the values of the resource properties. The PATCH HTTP method requires a request body. The body of the request must contain representation of the JSON Patch operations that you want to perform on the resource.
I'd suggest RFC 6902 as some light reading. It fleshes out a good way to handle PATCHing JSON resources.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With