Table of contents
- At the beginning
- What is HTTP response status codes?
- Which HTTP response status codes do beginners should know at first?
- 200 OK
- 201 Created
- 400 Bad Request
- 401 Unauthorized
- 403 Forbidden
- 404 Not Found
- 500 Internal Server Error
- Conclusion
At the beginning
When I first started learning web development, I was overwhelmed by lots of HTTP status codes - it seemed impossible for to memorize them all. However, as I continued developing more applications, I realized that there are only a couple of status codes that beginners really need to know at first.
This article would be the first step in learning this for beginners.
What is HTTP response status codes?
HTTP response status codes are 3-digit numbers that are part of the HTTP protocol which refers to the rules and conventions for exchanging information on the web.
They are returned by web servers to indicate the status of a client's request. The status codes are grouped into the following 5 categories.
- 1xx Informational
- 2xx Success
- 3xx Redirection
- 4xx Client Error
- 5xx Server Error
1xx Informational
This indicates that the request was received and is being processed(100 Continue, 102 Processing).
2xx Success
This indicates that the request was received, understood, and accepted successfully(200 OK, 201 Created, 204 No Content).
3xx Redirection
This indicates that further action needs to be taken by the client in order to complete the request(301 Moved Permanently, 302 Found, 304 Not Modified).
4xx Client Error
This indicates that there was a problem with the request from the client.(400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found).
5xx Server Error
This indicates that there was a problem on the server side in fulfilling the request(500 Internal Server Error, 501 Not Implemented, 503 Service Unavailable).
Which HTTP response status codes do beginners should know at first?
200 OK
This indicates that the request was successful. It's the most common success status code.
// When you can sign-in successfully
return res.status(200).json({ message: "Sign In out successfully" });
// When you can logout successfully
return res.status(200).json({ message: "Logout successfully" });
// When a client sends a GET request to retrieve a list of users
app.get('/users', (req, res) => {
const users = [/* fetch users from database */];
res.status(200).json(users);
});
201 Created
This is returned whenever a new resource is created on the server, for example, after a successful POST request to create a new record.
// When a client sends a POST request to create a new user
app.post('/users', (req, res) => {
const newUser = req.body;
// save newUser to database
res.status(201).json(newUser);
});
400 Bad Request
This indicates that the request wasn't formed correctly or invalid. It's a client-side error.
const { name, email } = req.body;
// Check if required fields are present
if (!name || !email) {
return res.status(400).json({ error: 'Name and email are required' });
}
401 Unauthorized
This indicates that the client has not been authenticated or has provided invalid credentials.
// When you fail the process of user authentication
return res.status(401).send({ message: "Authentication failed" });
// When the JWT token is missing from the request and respond
const token = req.cookies.token;
if (!token) return res.status(401).json({ message: "Not Authenticated!" });
403 Forbidden
This indicates that the client has provided valid authentication credentials, but it does not have permission or authorization to access the requested resource.
// When a non-admin user attempts to access some actions
if (user.isAdmin !== 'admin') {
return res.status(403).json({ error: 'Forbidden: Insufficient permissions' });
}
404 Not Found
This indicates that the requested resource does not exist on the server. It's still considered a client-side error, even though it may seem like it's related to the server. The reason for this is that when the client sends a request for a resource, it's the client's responsibility to request a valid resource that exists on the server.
// When user can't be found
const user = getUserById(userId);
if (!user) {
return res.status(404).json({ error: "User not found" });
}
// When comment can't be found
const comment = getCommentById(commentId);
if (!comment) {
return res.status(404).json({ error: "Comment not found" });
500 Internal Server Error
This is a generic server-side error code, indicating that something went wrong on the server, but the issue is unknown.
return res.status(500).json({ error: "Internal server error" });
Conclusion
It's really important for us to understand which HTTP status codes should be used when we create web applications.
Other articles are informative, so I summarize them in this article.
If you are interested in learning them, you can search more deeply.
Top comments (1)
HTTP code can be fun
I recommend these wonderful creations 😆
HTTP Status Dogs
HTTP Status Dogs. Hypertext Transfer Protocol Response status codes. And dogs.
HTTP Cats
API for HTTP Cats