HTTP Status Codes in REST
π‘ Concept Name
HTTP Status Codes in REST β Numeric codes sent by a server to indicate the result of a clientβs API request, such as success, failure, or redirection.
π Quick Intro
HTTP status codes help clients understand what happened after making a REST API call. They fall into categories like 2xx for success, 4xx for client errors, and 5xx for server errors.
π§ Analogy / Short Story
Imagine sending a package: a successful delivery gets a confirmation (200), a wrong address triggers a "not found" notice (404), and if the delivery service breaks down, you get an error report (500). Similarly, status codes communicate request outcomes.
π§ Technical Explanation
- β 2xx β Success: The request was received and processed successfully.
- π 3xx β Redirection: Further steps are needed to fulfill the request.
- β οΈ 4xx β Client Error: The request has issues caused by the client.
- π₯ 5xx β Server Error: The server failed to process a valid request.
- π Importance: Standard codes ensure consistent communication and help automate error handling in clients.
π― Purpose & Use Case
- β
Return
200 OK
for successful GET or PUT requests. - β
Use
201 Created
when a resource is successfully created with POST. - β
Use
204 No Content
when a request succeeds but doesnβt return data. - β
Respond with
400 Bad Request
for invalid client inputs. - β
Use
404 Not Found
when a resource does not exist. - β
Return
500 Internal Server Error
for unexpected server failures.
π» Real Code Example
// Node.js + Express example handling status codes
app.get('/user/:id', (req, res) => {
const user = users.find(u => u.id === parseInt(req.params.id));
if (!user) {
return res.status(404).json({ message: 'User not found' });
}
res.status(200).json(user);
});
app.post('/user', (req, res) => {
const user = req.body;
if (!user.name) {
return res.status(400).json({ message: 'Name is required' });
}
users.push(user);
res.status(201).json(user);
});

β Interview Q&A
Q1: What does status code 404 mean?
A: It means the requested resource was not found on the server.
Q2: When should 201 be returned in an API?
A: When a new resource is successfully created, usually after a POST request.
Q3: What does status code 500 indicate?
A: It signals an internal server error occurred while processing the request.
Q4: What does 204 No Content mean?
A: The request was successful but there is no content to return.
Q5: Is 400 a client or server error?
A: It is a client error, indicating the request was malformed or invalid.
π MCQs
Q1. What does 200 OK mean?
- Server error
- Redirect
- Not found
- Successful request
Q2. Which status code indicates resource creation?
- 200
- 204
- 404
- 201
Q3. Which category represents client errors?
- 2xx
- 3xx
- 4xx
- 5xx
Q4. What does 500 status code mean?
- Not found
- Created
- Internal server error
- Redirect
Q5. What is the meaning of 204 No Content?
- No content
- Not modified
- Bad gateway
- Conflict
Q6. Which status code means Bad Request?
- 401
- 403
- 400
- 404
Q7. What code means Not Found?
- 403
- 405
- 404
- 500
Q8. What status code is used for successful GET requests?
- 201
- 204
- 200
- 422
Q9. When is 403 Forbidden returned?
- Unauthorized
- Bad request
- Access is denied
- Resource created
Q10. Which status code is best for validation errors?
- 200
- 404
- 422
- 403
π‘ Bonus Insight
Extend standard HTTP status codes with custom error codes in your API responses to provide detailed error information, such as validation failures or specific error categories.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!