Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should the PATCH method return all fields of the resource in the response body?

Should the PATCH method return all fields of the resource in the response body?
Or should it return only updated fields?

I'm reading this

For example, if it returns only updated fields, the user could know which fields were updated in the server, while the user updated some fields.

**Users resource representations** name: string age: number createdon: date modifiedon: date 

PATCH /users/{userId} Request body {   name: 'changedname', } 

Response body Case1 {   name: 'changedname',   age: 20,   createdon: 2016-01-01,   modifiedon: 2016-06-09 } 

Response body Case2 {   name: 'changedname',   modifiedon: 2016-06-09 } 
like image 694
Nigiri Avatar asked Jun 09 '16 06:06

Nigiri


People also ask

What should a successful PATCH return?

According to RFC 5789 a successful response can return any success codes (i.e. 200 or 204).

Does PATCH method have body?

The PATCH HTTP method requires a request body. The body of the request must contain representation of the JSON Patch operations that you want to perform on the resource.

What does PATCH method do?

In computing, the PATCH method is a request method in HTTP for making partial changes to an existing resource. The PATCH method provides an entity containing a list of changes to be applied to the resource requested using the HTTP Uniform Resource Identifier (URI).

Does HTTP PATCH have body?

The HTTP PATCH request body describes how the target resource should be modified to produce a new version. Furthermore, the format used to represent the [description of changes] varies depending on the resource type. For JSON resource types, the format used to describe the changes is JSON Patch.


2 Answers

Normally this should be handled through content negotiation. In other words, the client asks for a specific representation if it needs one. The request would look like this:

PATCH /user/123 Content-Type: application/merge-patch+json Accept: application/vnd.company.user+json ... 

In this case, the client expresses that it wants a full user representation as answer. Or it could do:

PATCH /user/123 Content-Type: application/merge-patch+json Accept: application/vnd.company.object-fragment+json ... 

to ask for a generic fragment representation of some object.

You don't have to implement both if you don't want to, in which case you just do your use-case and respond with 406 Not Acceptable to media-types you do not support for the moment.

like image 158
Robert Bräutigam Avatar answered Sep 24 '22 09:09

Robert Bräutigam


The specification of PATCH doesn't mandate this.

If you want to control it you may want to look at https://greenbytes.de/tech/webdav/rfc7240.html#return for inspiration.

like image 33
Julian Reschke Avatar answered Sep 25 '22 09:09

Julian Reschke