HTTP/1.1 defines a number of numeric HTTP status codes that appear in the status line - the first line of an HTTP response - to summarise what the client should do with the response.
The first digit of a status codes defines the response’s class:
1xx
Informational2xx
Client request successful3xx
Request redirected - further action necessary, such as a new request4xx
Client error - do not repeat the same request5xx
Server error - maybe try againIn practice, it is not always easy to choose the most appropriate status code.
A HTTP 500 Internal Server Error is a general message meaning that the server encountered something unexpected. Applications (or the overarching web server) should use a 500 when there's an error processing the request - i.e. an exception is thrown, or a condition of the resource prevents the process completing.
Example status line:
HTTP/1.1 500 Internal Server Error
HTTP 404 Not Found means that the server couldn't find the path using the URI that the client requested.
HTTP/1.1 404 Not Found
Most often, the requested file was deleted, but sometimes it can be a document root misconfiguration or a lack of permissions (though missing permissions more frequently triggers HTTP 403 Forbidden).
For example, Microsoft's IIS writes 404.0 (0 is the sub-status) to its log files when the requested file was deleted. But when the incoming request is blocked by request filtering rules, it writes 404.5-404.19 to log files according to which rule blocks the request. A more detailed error code reference can be found at Microsoft Support.
Use 403 Forbidden when a client has requested a resource that is inaccessible due to existing access controls. For example, if your app has an /admin
route that should only be accessible to users with administrative rights, you can use 403 when a normal user requests the page.
GET /admin HTTP/1.1
Host: example.com
HTTP/1.1 403 Forbidden
Send an HTTP response with status code 200
to indicate a successful request. The HTTP response status line is then:
HTTP/1.1 200 OK
The status text OK
is only informative. The response body (message payload) should contain a representation of the requested resource. If there is no representation 201 No Content should be used.
Send a 304 Not Modified response status from the server send in response to a client request that contains headers If-Modified-Since
and If-None-Match
, if the request resource hasn’t changed.
For example if a client request for a web page includes the header If-Modified-Since: Fri, 22 Jul 2016 14:34:40 GMT
and the page wasn’t modified since then, respond with the status line HTTP/1.1 304 Not Modified
.
If-Modified-Since
or If-None-Match
.WWW-Authenticate
header field containing a challenge applicable to the requested resource.