Performing partial modifications to a resource
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
and PATCH
requests is reflected in the way the server processes the enclosed entity to modify the resource identified by the Request-URI. In a PUT
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. With PATCH
, 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 HTTP PATCH
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 [...]
Examples with JSON Patch
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.