HTTP status codes
When a client sends an HTTP request, the server describes the outcome in its response. A three-digit HTTP status code gives the client a quick way to understand that outcome.
The code can indicate success, a problem with the request, or a server error. The client can use it to decide what to do next: display the returned data after 200, for example, or show a “not found” message after 404.
A successful response might begin like this:
HTTP/1.1 200 OK
200 is the status code. OK is a human-readable description of it.
Status code categories
The first digit places the code in one of five categories.
| Category | Meaning | Examples |
|---|---|---|
1xx |
Informational response during processing | 101 Switching Protocols |
2xx |
Success | 200 OK, 201 Created |
3xx |
Redirection or related response handling | 301 Moved Permanently |
4xx |
Client error | 400 Bad Request, 404 Not Found |
5xx |
Server error | 500 Internal Server Error |
Browsers and HTTP libraries often handle informational responses and redirects automatically. When building web APIs, you will most often explicitly work with 2xx, 4xx, and 5xx codes.
Common status codes
| Status code | Meaning | Example |
|---|---|---|
200 OK |
The request succeeded. | Retrieve a list of posts or an individual post. |
201 Created |
A new resource was created. | Create a post or a user account. |
204 No Content |
The request succeeded with no response body. | Delete a post successfully. |
400 Bad Request |
The request is malformed or invalid. | Send incorrectly formatted data. |
401 Unauthorized |
Authentication is missing or invalid. | Call an authenticated API without valid credentials. |
403 Forbidden |
The server refuses the operation, often because permission is missing. | A regular user calls an admin-only API. |
404 Not Found |
The resource could not be found. | Request a nonexistent post. |
422 Unprocessable Content |
The content is understood but fails semantic validation. | Submit a post without a required field. |
500 Internal Server Error |
An unexpected server error occurred. | A bug prevents the server from completing a request. |
502 Bad Gateway |
A gateway received an invalid response from an upstream server. | A proxy receives a malformed upstream response. |
503 Service Unavailable |
The server is temporarily unable to handle the request. | Maintenance or excessive traffic. |
Status codes in REST APIs
Returning an appropriate code helps the client understand the result without having to interpret the response body first.
| Request | Success code | Meaning |
|---|---|---|
GET /posts |
200 OK |
The list of posts was retrieved. |
GET /posts/1 |
200 OK |
Post 1 was retrieved. |
POST /posts |
201 Created |
A new post was created. |
DELETE /posts/1 |
204 No Content |
Post 1 was deleted. |
By contrast, GET /posts/999 might return 404 Not Found if that post does not exist.
Exercises
Decide whether each statement is True or False.
200 OKmeans the server handled the request successfully.404 Not Foundmeans an unexpected internal server error occurred.401 Unauthorizedmeans the user is authenticated but lacks permission for the action.
Answers and explanations
- True —
200 OKindicates a successful request. - False —
404means the requested resource was not found. Unexpected internal errors usually use500. - False —
401means authentication is missing or invalid. An authenticated user lacking permission typically receives403 Forbidden.