Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP Status-code for empty response and response not found

We are implementing a REST based web service and we have some queries on some of the use cases.

Consider there is a unique account which contains some information (Ex. added to cart information)

  1. What response code should we return if no cart information exists (Ex. 0). Our understanding was to return 200 with empty response.
  2. User added cart information to his account, but cart is removed by admin.

What HTTP statuscode shall be used?

like image 906
Manigandan Seetharaman Avatar asked Apr 09 '26 15:04

Manigandan Seetharaman


1 Answers

For situation 1 there are two options:

  1. The cart is empty. I would return 200 OK returning an empty collection.
  2. The cart doesn't exist. The correct case for this is 404.

For situation 2, it's really the same. The only potential difference is that if you returned 404 for situation 1, you could choose 410 gone, as it indicates that a cart was here before, but it's now gone.

Regardless of which you choose, I would recommend you take the same strategy for both situations. E.g.: Either return a 2xx code for both or a 4xx code for both.

If the admin deleted the cart by doing a DELETE request, then the 404/410 status codes are more appropriate.

like image 153
Evert Avatar answered Apr 11 '26 06:04

Evert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!