REST: Mapping 404 HTTP Status codes
Asked Answered
D

2

13

Our team is developing RESTFul applications...we are debating the "BEST PRACTICE" approach.

Should 404 status code response be returned for a filter-like query? Say my GET URL is

.../1/service/startsWith/a.json

and it returns all values that start with A in my database...but if no "a" values are found should i just return status code 200 with an empty json string? or status code 404.

Thanks!

Delacroix answered 2/12, 2010 at 22:35 Comment(0)
R
15

See this question, in the update to my answer I address your issue. Specifically this bit,

I think the answer to whether to return 404 depends on the what is the resource that is being retrieved. Is it a representation of a search result, or is it a representation of a product? To know this you really need to look at the link relation that led us to the URL.

If the URL is supposed to return a Product representation then a 404 should be returned if the code does not exist. If the URL returns a search result then it shouldn't return a 404.

The end result is that what the URL looks like is not the determining factor. Having said that, it is convention that query strings are used to return search results so it is more intuitive to use that style of URL when you don't want to return 404s.

Rambouillet answered 2/12, 2010 at 23:49 Comment(1)
Yea, I agree with Darrel. Simply, "no results" is a valid search result. The resource exists, it's representation is simply "empty". So, 404 is not justified. Asking for a concrete resource by an identifier (/customers/123) that does not exist warrants a 404. That's not a query, that's more a "dereference" of a pointer.Akel
M
4

It may be more sensible to return a 204 code, which means 'No Content'. This would be slightly more efficient as a 204 status cannot have any document content, plus you can detect the code instead of having to parse the response.

Miff answered 23/7, 2013 at 12:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.