Reputation: 3387
A PUT
requests can have many outcomes, and I was wondering which status code would be best for each of them.
Say I want to create a new resources, so I do something like that:
PUT http://example.com/resources/resource-1
And I would get a HTTP 201
because a new resource has been created.
Now I want to update this resource, so I do the same request, with new contents:
PUT http://example.com/resources/resource-1
And I get HTTP 200
or HTTP 204
, because the resource has been updated. But what if I send this request once again with the same contents? Should the server return HTTP 200
or HTTP 204
even if nothing has been updated?
I am aware that HTTP 200
and HTTP 204
both just mean that the request was successfully processed, and even if the data don't change the request can (and should) still be successfully processed. But is there a way to tell the client that the request has been successfully processed but nothing has changed on the server side? And if there is, should it be used for a PUT
request? PUT
being idempotent, should different status be returned depending on the actual processing on the server side (as long as no error occurs during this processing)?
Upvotes: 22
Views: 32497
Reputation: 151588
But is there a way to tell the client that the request has been successfully processed but nothing has changed on the server side?
Yes, but that's not what status codes are for.
Either return 200 OK and a representation of the entity, or 204 No Content and return nothing.
For no change being applied, use a header like ETag. The client can compare the ETag with their previous value, and determine nothing was changed.
Upvotes: 21