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!

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: