M.Svrcek
M.Svrcek

Reputation: 5645

REST delete in reality update

i'm currently trying to build simple REST api with one endpoint or users looking like this:

GET /users - get all
GET /users/{id} - get one
POST /users - create
PUT /users/{id} - update 
DELETE /users/{id} - delete

The problem is with DELETE, because DELETE in reality only UPDATE user resource with status REMOVED. Is it ok? And which status should I return? I'm currently returning 202 Accepted, as i believe that saying "Resource is accepted to be removed" is ok, but maybe there should be just 200 OK

Upvotes: 0

Views: 46

Answers (3)

mathk
mathk

Reputation: 8143

The question that you have to ask yourself is:

Does the resource after the DELETE will still be available to the client?

If the answer is yes then I would suggest to use a PATCH you can even use the RFC6902 for that.

If the answer is no then DELETE is fine.

But since you have mention that the idea is to flag the user with a status delete I guess you have the intention to query all deleted user at some point or do something useful with it. Witch seems to be a client [admin] use case so PATCH sounds appropriate.

Regarding the status for DELETE. Some people may prefer to use the 204 No content since you are expecting the resource to be removed.

Upvotes: 1

Sapikelio
Sapikelio

Reputation: 2604

RestApiTutorial says this.

DELETE is pretty easy to understand. It is used to delete a resource identified by a URI.

On successful deletion, return HTTP status 200 (OK) along with a response body, perhaps the representation of the deleted item (often demands too much bandwidth), or a wrapped response (see Return Values below). Either that or return HTTP status 204 (NO CONTENT) with no response body. In other words, a 204 status with no body, or the JSEND-style response and HTTP status 200 are the recommended responses.

But you can use It for your prupose, if It do your Api logic easier to understand to the user.

I Hope It helps you.

Upvotes: 2

Emil Ingerslev
Emil Ingerslev

Reputation: 4785

When designing an API, you should always consider what the giving action means for the user of the API.

The user would not really want to know what happens behind the scenes, but only want a clean and understandable set of actions.

So for you delete action, you should consider, "will this delete the item, from the users perspective?"

Responding with status code 200 most often fine, but remember to send some content with it to support it. Using many different status code for success does not really give value, so be careful.

Upvotes: 2

Related Questions