We have an article resource with properties:
if we want only to remove image we make request
{title: null, image: null, description: null, status: null}
if we want only to update status we make request
{title: null, image: null, description: null, status: draft}
but in this case image also will be removed
How in REST to update only one property?
This capability allows a new HTTP method when exposing REST APIs, adding to the already available GET, POST, PUT, and DELETE methods. By definition, the PATCH method applies partial modifications to a resource, making it a lightweight option to PUT.
PUT is a method of modifying resource where the client sends data that updates the entire resource . PATCH is a method of modifying resources where the client sends partial data that is to be updated without modifying the entire data.
A PATCH is not necessarily idempotent, although it can be. Contrast this with PUT ; which is always idempotent. The word "idempotent" means that any number of repeated, identical requests will leave the resource in the same state.
The PATCH method is the correct choice here as you're updating an existing resource - the group ID. PUT should only be used if you're replacing a resource in its entirety.
The PATCH
method can be used to perform partial modifications to a resource. The request payload should contain a set of instructions describing how the resource will be modified. See the following quote from the RFC 5789:
2. The PATCH Method
The
PATCH
method requests that a set of changes described in the request entity be applied to the resource identified by the Request-URI. [...]The difference between the
PUT
andPATCH
requests is reflected in the way the server processes the enclosed entity to modify the resource identified by the Request-URI. In aPUT
request, the enclosed entity is considered to be a modified version of the resource stored on the origin server, and the client is requesting that the stored version be replaced. WithPATCH
, however, the enclosed entity contains a set of instructions describing how a resource currently residing on the origin server should be modified to produce a new version. [...]
To describe such set of instructions, you can use JSON Patch defined in the RFC 6902:
1. Introduction
JSON Patch is a format (identified by the media type
application/json-patch+json
) for expressing a sequence of operations to apply to a target JSON document; it is suitable for use with the HTTPPATCH
method.This format is also potentially useful in other cases in which it is necessary to make partial updates to a JSON document or to a data structure that has similar constraints [...]
To update the status, you can do the following:
PATCH /articles/1 HTTP/1.1
Host: example.com
Content-Type: application/json-patch+json
[
{ "op": "replace", "path": "/status", "value": "draft" }
]
Use the following to remove the image:
PATCH /articles/1 HTTP/1.1
Host: example.com
Content-Type: application/json-patch+json
[
{ "op": "remove", "path": "/image" }
]
And use the following to update the status and remove the image:
PATCH /articles/1 HTTP/1.1
Host: example.com
Content-Type: application/json-patch+json
[
{ "op": "replace", "path": "/status", "value": "draft" },
{ "op": "remove", "path": "/image" }
]
Alternatively to JSON Patch, you may want to consider JSON Merge Patch defined in the RFC 7396: it's also a means of describing a set of modifications to a target resource's content.
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