Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you indicate/request the deletion of an object property in a REST patch

Tags:

rest

Is there a standardized way to indicate a null value for a field/parameter in a RESTful PATCH request?

Let's say I have an object, and I want to just flat out remove a property from the object and persist that change to the MT/DB. On the client side, I would just delete the property (in JS), but when PATCHing to my REST API layer, is there a standardized way to indicate that the property (not the object, just the property) should be deleted?

For the sake of clarity/example, here is a hypothetical PATCH route for the object:

PATCH /service/thing/{id}

and some sample PATCH payloads:

single property update:

{ "myprop": "changed" }

multi-property update:

{ "myprop": "changed", "anotherprop": "changed" }

For a PATCH where you want to get rid of "myprop" you obviously can't just submit this payload:

{ }

Would you do something like:

{ "myprop": undefined }

or, would you create a whole new DELETE route:

DELETE /service/thing/{id}/myprop

or, something else?

like image 656
cmcculloh Avatar asked Oct 21 '22 22:10

cmcculloh


1 Answers

I would implement PATCH just like you propose. In a way that accepts partial representation for update.

This way I would send always the JSON partial representation.

PATCH { "myprop": "newvalue" }

And for deletion of the myprop the empty value. Sending back the empty value when requesting a GET is up to you. You can always ignore null values (which is not the same as empty).

PATCH { "myprop": null }

Note null rather than undefined. null is a JSON value. But this is just my 2cents.

like image 56
ssedano Avatar answered Jan 02 '23 21:01

ssedano