6 ms·
returning a null array to represent no items To be honest, that's what I'd expect. What do you dislike about that result, and what would you prefer to see retu
by akurtzhs 7y ago
returning a null array to represent no items
To be honest, that's what I'd expect. What do you dislike about that result, and what would you prefer to see returned? Jump straight to 404?
returning a single object without an array to represent one item.
So an array if there's multiple results, and a bare object for a single result? That's unpleasant.
- saagarjha 7y ago> What do you dislike about that result, and what would you prefer to see returned? It’s inconsistent and means I need to write special case code to check for it, when before I could choose to. It should return an empty array.
- irishsultan 7y agonull array sounds like it's an array (presumably an empty one), it would have been clearer if OP had said null instead.
- saw-lau 7y agoNot sure why you're getting downvoted - that was how I read it as well.
- bobbylarrybobby 7y agoPresumably they would prefer an array always be returned. If zero items, an empty array; if one item, an array of length one; if more items, a longer array.
- deleted 7y ago[deleted]
- Sohcahtoa82 7y agoIf an API is expected to return an array, but the data set is empty, I expect an empty array. I should be able to go straight from the request to a for-each loop without checking for null.
- ergothus 7y ago> Jump straight to 404? That's my pet peeve. I completely understand the logic - using the HTTP status code to show the result of interaction with the resource is very RESTful - but my complaint is that unless you provide more context, a 404 doesn't tell me if I'm getting a response of "that resource doesn't exist" vs "you're accessing a url pattern that will never work". So if you want this random internet user to be pleased, never return a 404 response unless the caller is able to see this difference. (Usually having SOME form of body unique to the API is sufficient to prove the 404 is not because of a bad API call). Giving me the HTML to your site 404 page when I called a non-HTML API is likewise sad-inducing.
- WrtCdEvrydy 7y agoWe have only one pattern like this... when there's a client resource (containing personal information for contacting them) that the client has asked to be private... we return an empty JSON object (versus an object with the contact information keys)
- thaumasiotes 7y ago>> returning a null array to represent no items > To be honest, that's what I'd expect. What do you dislike about that result, and what would you prefer to see returned? Jump straight to 404? I'd expect an empty array to represent no items. It's the difference between "" and "[]".
- ako 7y ago404 indicates an error, if the result is just empty but not erroneous 204 "no content" may be better.
- wwweston 7y ago+1, 204 is underutilized for no-result cases.
- jbverschoor 7y agoIn the browser it will also do no page refresh as there is nothing to render.