HTTP Status Codes

Return status codes that accurately describe successful and failed HTTP operations.

Lesson content

🎯 Goal By the end of this lesson, you'll understand: What are HTTP Status Codes? Why do we use them? Status Code Categories Most Common HTTP Status Codes Real-world examples Interview questions What are HTTP Status Codes? HTTP Status Codes are 3-digit numbers returned by a server to indicate the result of a client's request. Simple Definition: HTTP Status Codes tell the client whether a request was successful, failed, or needs further action. Why Do We Need HTTP Status Codes? When a client sends a request to a server, the server must tell the client what happened. Example: ✅ Request successful ❌ Resource not found 🔒 Access denied ⚠️ Server error Status codes provide this information. Real-Life Example 📬 Imagine sending a letter. You Send Letter │ ▼ Post Office │ ▼ Response Delivered Successfully ✅ Wrong Address ❌ Receiver Not Available ⚠️ Similarly, HTTP returns status codes. Status Code Categories Range Meaning 1xx Informational 2xx Success 3xx Redirection 4xx Client Error 5xx Server Error 1xx – Informational The request has been received and processing continues. Common Codes Code Meaning 100 Continue 101 Switching Protocols 2xx – Success The request completed successfully. 200 OK GET /users Response 200 OK 201 Created Used after creating a new resource. POST /users Response 201 Created 204 No Content Request succeeded but no response body is returned. DELETE /users/10 Response 204 No Content 3xx – Redirection The client needs to perform another action. Common Codes Code Meaning 301 Moved Permanently 302 Found (Temporary Redirect) 304 Not Modified 4xx – Client Errors The problem is with the client's request. 400 Bad Request Invalid request. POST /users Missing required fields. Response 400 Bad Request 401 Unauthorized Authentication is required or failed. 401 Unauthorized 403 Forbidden User is authenticated but not allowed. 403 Forbidden 404 Not Found Requested resource does not exist. 404 Not Found 405 Method Not Allowed Wrong HTTP method used. Example POST /users But API supports only: GET /users Response 405 Method Not Allowed 409 Conflict Request conflicts with the current state. Example Trying to register an email that already exists. 409 Conflict 422 Unprocessable Content The request format is valid, but the data fails validation. Example { "email": "invalid-email" } Response 422 Unprocessable Content 5xx – Server Errors The request is valid, but the server failed. 500 Internal Server Error Unexpected server error. 500 Internal Server Error 502 Bad Gateway One server received an invalid response from another server. 502 Bad Gateway 503 Service Unavailable Server is temporarily unavailable. 503 Service Unavailable 504 Gateway Timeout A server acting as a gateway did not receive a timely response from an upstream server. 504 Gateway Timeout Most Common Status Codes Code Meaning When to Use 200 OK Successful GET 201 Created Resource created 204 No Content Successful delete/update with no body 400 Bad Request Invalid request 401 Unauthorized Authentication required/failed 403 Forbidden No permission 404 Not Found Resource missing 409 Conflict Duplicate resource/conflict 422 Unprocessable Content Validation failed 500 Internal Server Error Unexpected server error Express.js Example app . get( "/users" , ( req , res ) => { res . status( 200 ) . json({ message: "Success" }); }); Create User app . post( "/users" , ( req , res ) => { res . status( 201 ) . json({ message: "User Created" }); }); Not Found app . use(( req , res ) => { res . status( 404 ) . json({ message: "Route Not Found" }); }); Request Flow Client │ ▼ HTTP Request │ ▼ Server │ ▼ Process Request │ ▼ HTTP Status Code │ ▼ Client Real-Life Example 🛒 Online Shopping Place Order │ ▼ 200 → Order Details Retrieved 201 → Order Created 404 → Product Not Found 401 → Login Required 500 → Server Error Interview Question Which status code should be returned after creating a new user? Answer 201 Created Which status code is returned when authentication fails? Answer 401 Unauthorized Which status code is used when the user doesn't have permission? Answer 403 Forbidden Best Practices ✅ Use 200 for successful GET requests. ✅ Use 201 after creating resources. ✅ Use 204 when no response body is needed. ✅ Use 400 for invalid requests. ✅ Use 401 for authentication failures. ✅ Use 403 for permission issues. ✅ Use 404 when a resource doesn't exist. ✅ Use 422 for validation errors. ✅ Use 500 for unexpected server errors. Key Points ✅ HTTP Status Codes are 3-digit server responses. ✅ 1xx = Informational. ✅ 2xx = Success. ✅ 3xx = Redirection. ✅ 4xx = Client Errors. ✅ 5xx = Server Errors. ✅ Choosing the correct status code makes APIs easier to understand and debug. 🧠 Easy Formula Request Sent │ ▼ Server │ ▼ HTTP Status Code │ ▼ Client 1xx → Information 2xx → Success 3xx → Redirect 4xx → Client Error 5xx → Server Error 200 → Success 201 → Created 204 → No Content 400 → Bad Request 401 → Unauthorized 403 → Forbidden 404 → Not Found 409 → Conflict 422 → Validation Error 500 → Server Error Remember: ✅ 2xx = Success 🔄 3xx = Redirect ❌ 4xx = Client made a mistake 💥 5xx = Server made a mistake 🚀 Using the correct HTTP status code is a REST API best practice.