2

What HTTP status code is better to use in the following case?

I have a REST API to get some database entity basing on its id. The case is when the requested entity cannot be found in the database.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
sys463
  • 337
  • 2
  • 5
  • 18

2 Answers2

3

I tend to use 404. That being said here's a wikipedia link for HTTP status codes https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

DJN
  • 31
  • 3
  • 3
    @sys463 Your API *resource* is the db entity. https://stackoverflow.com/questions/10799198/what-are-rest-resources –  Oct 25 '18 at 20:10
2

404 is the status code suitable for this situation. It indicates a client error and indicates that a representation cannot be found for requested resource.

From the RFC 7231, the document that defines the semantics of the HTTP/1.1 protocol:

6.5.4. 404 Not Found

The 404 (Not Found) status code indicates that the origin server did not find a current representation for the target resource or is not willing to disclose that one exists. A 404 status code does not indicate whether this lack of representation is temporary or permanent; the 410(Gone) status code is preferred over 404 if the origin server knows, presumably through some configurable means, that the condition is likely to be permanent.

Community
  • 1
  • 1
cassiomolin
  • 124,154
  • 35
  • 280
  • 359