HTTP status code for PUT

A PUT requests can have many results, and I was wondering which status code would be best for each of them.

Let's say I want to create new resources, so I am doing something like this:

 PUT http://example.com/resources/resource-1 

And I would get HTTP 201 because a new resource was created.

Now I want to update this resource, so I am making the same request with the new content:

 PUT http://example.com/resources/resource-1 

And I get HTTP 200 or HTTP 204 because the resource is updated. But what if I send this request again with the same content? Should the server return HTTP 200 or HTTP 204 even if nothing is updated?

I know that HTTP 200 and HTTP 204 both simply mean that the request was successfully processed, and even if the data does not change, the request can (and should) still be successfully processed. But is there a way to tell the client that the request was successfully processed, but nothing has changed on the server side? And if so, should it be used to request a PUT ? PUT is idempotent, should a different status be displayed depending on the actual processing on the server side (if there is no error during this processing?)

+7
rest
source share
1 answer

But is there a way to tell the client that the request was 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 the representation of the object, or 204 No Content and return nothing.

To prevent changes from being applied, use a header such as ETag. The client can compare the ETag with their previous value and determine that nothing has changed.

+8
source

All Articles