0

My team and myself are trying to decide on what Status Code to use, our API Client Generated via Swagger has a condition for 204 (No Content) which has more meaning than a 404 in this instance.

We want to show that the response was successful but there was no Object returned.

Let's say it's a shopping cart: The customer clicks on the Cart and takes you to the page, the request sends to the endpoint Get//CartOrderDetails but there is no items in the cart, we don't want it to run through the API Client's 200 path, but neither do we want it to go through the 404 path since it wasn't Not Found.

What would you guys use in this case?

Edit: Thanks for all the responses overnight! Definitely helped put things in light, seems there will always be an argument for and against 204. Thanks guys!

  • 2
    hmm it doesn't make sens ... you should return empty array ... – Selvin Jul 19 '21 at 13:56
  • 2
    It really depends on your design philosophy. I would go with 200 and a model in the body, sth like `{"cartItems":[]}` which confirms to the client that a) their request was understood b) they can understand the format c) there was no networking issues. And the fact that the cart was empty has nothing to do with the underlying HTTP protocol. Then again, others will disagree. – zaitsman Jul 19 '21 at 13:56
  • 3
    You can return a 200 with an empty result set (e.g. `[]`, or 204 is also valid. 204 is absolutely not the same as 404. – DavidG Jul 19 '21 at 13:57
  • 2
    An empty cart is a container without any items => like an empty array. So you should return 200 and an empty array in this case – Sir Rufo Jul 19 '21 at 13:58
  • 1
    204 would be pain in the a... on the client side fx image code like `var result = await httpClient.GetFromJsonAsync>(...)` I'm pretty sure that it would not handle 204 – Selvin Jul 19 '21 at 14:00
  • BTW. [Http Status 204](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/204) – Sir Rufo Jul 19 '21 at 14:00
  • What makes you say that @Selvin? – mjwills Jul 19 '21 at 14:00
  • @Selvin I'm pretty sure every HTTP clients handle `204` the same as `200`. – Marco Jul 19 '21 at 14:09
  • It's not about http client but json parsing librares like `Retrofit` on java or `System.Net.Http.Json` in C# ... They tend to care less about HTTP codes – Selvin Jul 19 '21 at 14:12
  • Does this answer your question? [Proper REST response for empty table?](https://stackoverflow.com/questions/13366730/proper-rest-response-for-empty-table), [HTTP status code for a REST search without result](https://stackoverflow.com/q/45152551/113116), [Which HTTP code to use for an empty subresource in a REST API?](https://stackoverflow.com/q/13564031/113116), [correct status code / description returning empty collection as web response](https://stackoverflow.com/q/32986959/113116), [What http return code should be if no data available](https://stackoverflow.com/q/38659310/113116) – Helen Jul 20 '21 at 06:27

1 Answers1

0

What would you guys use in this case?

we don't want it to run through the API Client's 200 path

For REST, that's the wrong thing to want.

Uniform interface is an important REST constraint. Among other things, that means that all resources use self descriptive messages with the same semantics.

The status-code element is a 3-digit integer code describing the result of the server's attempt to understand and satisfy the client's corresponding request. The rest of the response message is to be interpreted in light of the semantics defined for that status code. -- RFC 7230

In other words, the status-code is meta data of the transfer of documents over a network domain. You should be choosing the status code based on the semantics of your response message, not on how you want that message to be handled.

If what you are sending back to the client is a representation of the resource requested, then you should be returning 200.

If the representation of the resource happens to be zero bytes long, you might get away with sending 204 instead of 200, but that isn't really what it is for. The semantics of 204 center a corner case from remote authoring.

Of course, if your response includes a body, then using a 204 status code is likely to make a mess; so don't do that.

200 OK
Content-Type: application/json

null
VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91