Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way of handling PATCH requests on array properties?

Tags:

rest

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-

like image 803
Pez Avatar asked May 08 '14 02:05

Pez


People also ask

When should we use the PATCH HTTP method?

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.

Where should we use put and PATCH HTTP methods?

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.

What are PATCH requests?

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.

What is the use of PATCH method in REST API?

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.


1 Answers

I'd suggest RFC 6902 as some light reading. It fleshes out a good way to handle PATCHing JSON resources.

like image 126
Eric Stein Avatar answered Oct 23 '22 03:10

Eric Stein