Response Code in HTTP
An HTTP response code, or status code, is the three-digit number a server sends at the start of every response to say how the request went. Your code reads it to decide what to do next: show the data, ask the user to log in, display a "not found" message, or try again later.
The five families
The first digit tells you the category, so even an unfamiliar code is easy to interpret.
| Range | Meaning | Typical examples |
|---|---|---|
| 1xx | Informational, the request is in progress | 101 Switching Protocols |
| 2xx | Success | 200 OK, 201 Created, 204 No Content |
| 3xx | Redirection, look somewhere else | 301 Moved Permanently, 304 Not Modified |
| 4xx | Client error, the request had a problem | 400, 401, 403, 404, 422, 429 |
| 5xx | Server error, the server failed | 500, 502, 503 |
The codes you will handle most
- 200 OK: the standard success, with data in the body.
- 201 Created: a
POSTcreated a new resource. The body often contains the new item. - 204 No Content: success with an empty body, common after a
DELETE. Callingres.json()on it will throw. - 400 Bad Request: the data sent was malformed or failed validation.
- 401 Unauthorized: the user is not authenticated. Despite the name, it means "we do not know who you are".
- 403 Forbidden: the server knows who you are, but you are not allowed to do this.
- 404 Not Found: no resource exists at that path.
- 429 Too Many Requests: you hit a rate limit; wait before retrying.
- 500 Internal Server Error: a bug or crash on the server. The client usually cannot fix it.
fetch does not reject on error codes
This is the single biggest trap for front-end developers. The fetch promise rejects only when the request could not be completed at all, such as no network connection. A 404 or 500 is still a completed response, so fetch resolves normally. You must check response.ok (true for 200 to 299) or response.status yourself.
async function loadCourse(id) {
const res = await fetch(`/api/courses/${id}`);
if (res.status === 404) {
return null; // show a "not found" message
}
if (res.status === 401) {
throw new Error('Please log in again.');
}
if (!res.ok) {
throw new Error(`Server error: ${res.status}`);
}
return res.json();
}
Libraries such as Axios behave differently and reject for any status outside the 2xx range, so read the documentation of whatever client you use.
Choosing the right code on the server
If you write the API yourself, in Express for example, pick codes that match what happened. A classic bad practice is returning 200 with a body like {"error": "Not found"}. It forces every client to inspect the body, breaks caching and monitoring, and hides failures. Send res.status(404).json({ message: 'Course not found' }) instead.
Also avoid leaking details in 500 responses. Log the stack trace on the server and send the client only a short, general message.
Reading codes in practice
The browser's Network tab colours failed requests red and shows the code for each one. When something breaks, check the status first: a 4xx points at what the client sent, a 5xx points at the server.
To sum up: read the first digit for the category, check res.ok after every fetch, and return honest, specific codes from your own APIs.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.