Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Response to a PATCH request

Suppose I have an endpoint /api/todos and I make a PATCH request to change one of the todos. Does it ever make sense for my PATCH request to have in the response body the list of all of the todos?

Is there any specification that I can read about this?

I have talked to some developers and the general consensus is that the PATCH method should only return the changed entry. I agree with this point but am looking for documentation detailing this.

like image 436
A. Wong Avatar asked Sep 25 '17 14:09

A. Wong


People also ask

What should be the response of a PATCH API?

According to RFC 5789 a successful response can return any success codes (i.e. 200 or 204). It should ideally also contain an ETag header to allow clients to track the current version for eventual consecutive requests (which is basically used for optimistic locking on the resources state).

What should a PATCH request return?

"value" represents the amount being added to the existing resource. Before applying the changes in the PATCH document, the server has to check whether the PATCH document received is appropriate for the requested resource. If the PATCH request succeeds then it returns a 204 response.

What does it mean when a PATCH request?

A PATCH request is considered a set of instructions on how to modify a resource. Contrast this with PUT ; which is a complete representation of a resource. A PATCH is not necessarily idempotent, although it can be. Contrast this with PUT ; which is always idempotent.


3 Answers

The RFC is here.

But it doesn't really specify anything. Here's an example from it:

   Successful PATCH response to existing text file:

   HTTP/1.1 204 No Content
   Content-Location: /file.txt
   ETag: "e0023aa4f"

   The 204 response code is used because the response does not carry a
   message body (which a response with the 200 code would have).  Note
   that other success codes could be used as well.

So you can implement it however you wish. Responding with just the changed entry would be more inline with how other methods (PUT, DELETE, etc.) are usually handled, so I would do it that way.

like image 73
Nick Avatar answered Nov 10 '22 02:11

Nick


So when you want to provide a RESTfull api /api/todos represents a ressource where every todo has an separate id. Here you would patch e.g. to `/api/todos/4' to update the todo with the id 4. The usual behaviour here would be to responde with either a 204 Status Code or with the updated object to save another request to get the updated todo.

If your todo's aren't separate objects with id's they are propably a list of items and should be returned all at once.

like image 38
Johannes Reichard Avatar answered Nov 10 '22 00:11

Johannes Reichard


I like to use the Google API AIPs for most of my api development.

https://google.aip.dev/134

Small excerpt from AIP 134 which is about updates and PATCH.

  • The response message must be the resource itself.
  • The response should include the fully-populated resource, and must include any fields that were sent and included in the update mask unless they are input only (see AIP-203).
like image 44
Hubbard.ZLee Avatar answered Nov 10 '22 02:11

Hubbard.ZLee