REST API 404: Bad URI, or Missing Resource?

Brian Lacy picture Brian Lacy · Mar 29, 2012 · Viewed 230.2k times · Source

I'm building a REST API, but I've encountered a problem.

It seems that accepted practice in designing a REST API is that if the resource requested doesn't exist, a 404 is returned.

However, to me, this adds unnecessary ambiguity. HTTP 404 is more traditionally associated with a bad URI. So in effect we're saying "Either you got to the right place, but that specific record does not exist, or there's no such location on the Internets! I'm really not sure which one..."

Consider the following URI:

http://mywebsite/api/user/13

If I get a 404 back, is that because User 13 does not exist? Or is it because my URL should have been:

http://mywebsite/restapi/user/13

In the past, I've just returned a NULL result with an HTTP 200 OK response code if the record doesn't exist. It's simple, and in my opinion very clean, even if it's not necessarily accepted practice. But is there a better way to do this?

Answer

Robert Levy picture Robert Levy · Mar 29, 2012

404 is just the HTTP response code. On top of that, you can provide a response body and/or other headers with a more meaningful error message that developers will see.