Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST: Mapping 404 HTTP Status codes

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!

like image 803
Jeryl Cook Avatar asked Dec 02 '10 22:12

Jeryl Cook


2 Answers

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.

like image 93
Darrel Miller Avatar answered Sep 20 '22 18:09

Darrel Miller


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.

like image 44
Phil Kingston Avatar answered Sep 18 '22 18:09

Phil Kingston