HTTP Status-code for empty response and response not found
10,970
For situation 1 there are two options:
- The cart is empty. I would return
200 OK
returning an empty collection. - 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.
Related videos on Youtube
![Manigandan Seetharaman](https://lh6.googleusercontent.com/-FMr1iFTM_wA/AAAAAAAAAAI/AAAAAAAAAAs/ISdfdEIraEI/photo.jpg?sz=256)
Author by
Manigandan Seetharaman
Updated on June 04, 2022Comments
-
Manigandan Seetharaman about 2 years
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)
- What response code should we return if no cart information exists (Ex. 0). Our understanding was to return 200 with empty response.
- User added cart information to his account, but cart is removed by admin.
What HTTP statuscode shall be used?
-
Sunil Kanzar almost 6 yearsIt is good to return
204 No Content
if there is no data to return -
Namrata Das almost 6 yearsI agree, good point. But only if you're literally sending 0 bytes. If you're sending an empty JSON array for example, you shouldn't.
-
Air almost 4 yearsI don't think it is appropriate to return a 404 error code if requesting an empty cart. After all there was no error, just simply an alternate circumstance. If requesting the cart of user2241 and that user doesn't exists, then for sure: 404 Not Found, user2241 could not be found. There may be some variables in terms of how carts have been set up, but in most cases all users have a cart, empty or not. Deleting the last item should not delete the cart, and adding the first item should not need to create a cart. Just cart lines. Requesting an empty cart should return an empty array and 200/204