Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PATCH in REST and null

We have an article resource with properties:

  • title
  • image
  • description
  • status: published | draft

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?

like image 519
Combo Avatar asked Apr 04 '18 09:04

Combo


People also ask

What is PATCH in REST API?

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.

What is difference between PATCH and put in rest?

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.

Is PATCH idempotent in rest?

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.

Should I use PATCH or put?

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.


1 Answers

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.

like image 193
cassiomolin Avatar answered Nov 11 '22 02:11

cassiomolin