Reputation: 5654
Question on best practice on REST API design
Suppose I want to update only one property(lastN) of customer
{id:1,firstN:fb,lastN:ln,dept:201,region:NA}
then one can/should send
{id:1,lastN:newLn}
with PATCH/PUT or both ?
{id:1,firstN:fb,lastN:newLn,dept:201,region:NA}
with PUT/PATCH or both ?
I have already asked similar(not same) question here, and want to know the answer for this also.
Upvotes: 1
Views: 3668
Reputation: 8503
PUT should be used to update the whole resource. For partial updates PATCH is the correct verb.
However, PATCH is not a straight replacement for PUT. If you only want to send partial properties to add/update, the content type "application/merge-patch+json" should be used. The body would only contain modified properties.
https://www.rfc-editor.org/rfc/rfc7396
Content-Type: application/merge-patch+json
{firstN:newFn,region:null,middleName:newMn}
Another, more descriptive alternative is to use JSON Patch document that actually defines operations.
https://www.rfc-editor.org/rfc/rfc6902#section-3
Content-Type: application/json-patch+json
[
{ "op": "add", "path": "/middleName", "value": newMn },
{ "op": "remove", "path": "/region" },
{ "op": "replace", "path": "/firstN", "value": newFn },
]
Upvotes: 5