Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

404 Not Found or Bad Request?

Let's say that we have the following REST call:

GET api/companies/5  

(get company with id 5)

If company '5' doesn't exist, we would typically return a 404 Not Found response.

But now, let's take this call:

GET api/companies/5/invoices/10  

(get invoice 10 from company 5)

Now, if company '5' doesn't exist, do we still return a 404 Not Found? Or should a 404 only be returned if the outer most resource can not be found (invoice 10, in this case).

Would Bad Request perhaps be a better option?

like image 291
Dave New Avatar asked Nov 17 '14 08:11

Dave New


People also ask

What is the difference between bad request and not found?

A 400 Bad Request means: The request could not be understood by the server due to malformed syntax. Whereas, 404 states: The server has not found anything matching the Request-URI.

What causes 404 page not found?

A 404 error indicates that the webpage you're trying to reach can't be found. You might see a 404 error because of a problem with the website, because the page was moved or deleted, or because you typed the URL wrong.


2 Answers

404 is your best response. According to the HTTP RFC, http://www.ietf.org/rfc/rfc2616.txt,

A 400 Bad Request means:

The request could not be understood by the server due to malformed syntax.

Whereas, 404 states:

The server has not found anything matching the Request-URI.

The entire URI is your resource identifier, and you're not finding a matching resource for that particular identifier.

like image 171
Josh Rack Avatar answered Sep 22 '22 19:09

Josh Rack


404 may cause a confusion - is the resource missing or is the actual URL incorrect?

I'd personally go for the 422 code:

   The 422 (Unprocessable Entity) status code means the server    understands the content type of the request entity (hence a    415(Unsupported Media Type) status code is inappropriate), and the    syntax of the request entity is correct (thus a 400 (Bad Request)    status code is inappropriate) but was unable to process the contained    instructions.  For example, this error condition may occur if an XML    request body contains well-formed (i.e., syntactically correct), but    semantically erroneous, XML instructions. 
like image 27
Mārtiņš Briedis Avatar answered Sep 22 '22 19:09

Mārtiņš Briedis