0

I am working on a one page web application for an online shop in Angular which will rely on a REST API handled by Symfony. One of the first request I make is that I need to know if the customer has an active cart or not. So I have a route /get-user-cart which returns null if the user has no cart (then I display a button to create a cart), if the user has an active cart I return the cart and its content.

My question is what HTTP code should I return if the user has no cart? Should I return a 200 code (the server answered correctly and the response is null) or a 404 content not found code?

dippas
  • 58,591
  • 15
  • 114
  • 126
Magnesium
  • 597
  • 6
  • 22
  • Does this answer your question? [HTTP status code for update and delete?](https://stackoverflow.com/questions/2342579/http-status-code-for-update-and-delete) – KhorneHoly Jul 24 '20 at 09:33
  • `200` looks correct. – nice_dev Jul 24 '20 at 09:43
  • Sorry i don't think the link answers to it, first it's a get and not put or delete. Second the fact that there is no cart is not an error per say. There is no cart is as valid as here is your cart. – Magnesium Jul 24 '20 at 09:50
  • Most sites don't really require you to *create* a cart. Why not just return 200 with an empty array to indicate the cart is empty? – ceejayoz Jul 24 '20 at 16:01

2 Answers2

1

Ideally you should return 200 status code with null response. Since cart is not a resource, Where as each of the contents inside carts can be considered as resource.

Rahul Cv
  • 725
  • 5
  • 12
  • cart can be considered as a property of the resource user. In your case resources are 1. User 2. Products – Rahul Cv Jul 24 '20 at 09:53
-1

I would return a 404.

Think that you are requesting an object (the user cart) to te API and the object does not exists.

David Rojo
  • 2,337
  • 1
  • 22
  • 44
  • Yes but i am not specifically requesting a cart, i am asking does a cart exist and if it does please return it to me. – Magnesium Jul 24 '20 at 09:40
  • Yes, that's really a point of view. But what you are really requesting is "Give me the cart of the current user" is like "Give me the cart with id XXX" but the id is implicit in the user session. Returning a 404 you are saying to your app "This resource does not exists" and then you can create it (witht a POST request), but returning a 200 response with null you are saying "This resource exists but is empty" – David Rojo Jul 24 '20 at 09:52
  • @DavidRojo I don't understand why someone downvoted you. This is the correct answer. also the OP uses the endpoint getFoo rather than findFoo. Find can yield empty results, but get can only return an object or throw an exception, in this case, a 404 – meewog Jul 24 '20 at 11:17
  • https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404 – meewog Jul 24 '20 at 11:21