REST Response Code for DELETE Operation, 200 or 204 ? - DevDummy


Views | Thoughts | Concepts | Techniques

Wednesday, March 14, 2018

REST Response Code for DELETE Operation, 200 or 204 ?

Should we use 200 OK or 204 No Content as per the response for Delete Operation ?

It is often arguable whether we should use 200 or 204 as response code.

When to use 204

204 Definition
The server has successfully fulfilled the request and that there is no additional content to send in the response payload body.

If you are doing a removal of a data in a non reversible way, the system permanently loses the data, a permanent delete.

Example : Delete a transaction entry of a particular transaction, identifying it as a duplicate or some other reason.

In such a case, 204 No Content can be used.

When to use 200

200 Definition
200 OK
The request has succeeded.

The payload sent in a 200 response depends on the request method. For the methods defined by this specification, the intended meaning of the payload can be summarized as:

If you are making a soft delete by marking the data, such as setting a flag.

Example : Delete a User who already has relations to Transactions. In this case deleting this user will cause several issues as it has several relations such as transaction and etc. Here the User needs to be marked as deleted by a flag or by setting STATUS to INACTIVE/ DELETED . 

Here it is very important to  handle this in the application to skip INACTIVE users on related activities.


No comments:

Post a Comment