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.
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).
"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.
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.
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.
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.
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.
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