0

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?

rd3n
  • 3,676
  • 1
  • 26
  • 41

2 Answers2

0

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.

Evert
  • 75,014
  • 17
  • 95
  • 156
-2

See This Blog. It explains it very well.

Summary of the blog's comments on 204:

  1. 204 No Content is not terribly useful as a response code for a browser (although according to the HTTP spec browsers do need to understand it as a 'don't change the view' response code).
  2. 204 No Content is however, very useful for ajax web services which may want to indicate success without having to return something. (Especially in cases like DELETE or POSTs that don't require feedback).

The answer, therefore, to your question is use 404 in your case. 204 is a specialized reponse code that you shouldn't often return to a browser in response to a GET.

The other response codes are even less appropriate than 204 and 404.

  1. 200 should be returned with the body of whatever you successfully fetched. Not appropriate when the entity you're fetching doesn't exist.
  2. 202 is used when the server has begun work on an object but the object isn't fully ready yet. Certainly not the case here. You haven't begun, nor will you begin, construction of user 9 in response to a GET request. That breaks all sorts of rules.
  3. 400 is used in response to a poorly formatted HTTP request (for instance malformed http headers, incorrectly ordered segments, etc). This will almost certainly be handled by whatever framework you're using. You shouldn't have to deal with this unless you're writing your own server from scratch. Edit: Newer RFCs now allow for 400 to be used for semantically invalid requests.

Wikipedia's description of the HTTP status codes are particularly helpful. You can also see the definitions in the HTTP/1.1 RFC2616 document at www.w3.org

NAVIN
  • 2,514
  • 4
  • 15
  • 30
  • I 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 – Air Jul 15 '20 at 17:02