CHAPTER 03
Beginner
HTTP Methods and Status Codes
Updated: May 14, 2026
25 min read
# CHAPTER 3
HTTP Methods and Status Codes
1. Introduction
When you speak a language, you need verbs to describe actions and nouns to describe objects. In the language of the internet (HTTP), the URLs are the nouns, and the HTTP Methods are the verbs. When a Server replies, it doesn't use English words; it uses a standardized system of 3-digit numbers called HTTP Status Codes. In this chapter, we will master the core verbs of REST (GET, POST, PUT, DELETE) and learn how to interpret and return the correct status codes to guarantee predictable communication.2. Learning Objectives
By the end of this chapter, you will be able to:- Identify the four primary HTTP Methods used in REST APIs.
- Map HTTP Methods to database CRUD operations.
- Understand the 5 classes of HTTP Status Codes (1xx to 5xx).
- Select the appropriate Status Code for successful and failed API operations.
3. Beginner-Friendly Explanation
Imagine a digital filing cabinet.- You want to read a file? You use GET.
- You want to add a brand new file? You use POST.
- You want to replace an existing file with a revised version? You use PUT.
- You want to destroy a file? You use DELETE.
When the cabinet responds, it flashes a light:
- Green Light (200s): "Success! I did what you asked."
- Yellow Light (400s): "You made a mistake. The file you asked for doesn't exist, or you lack the security clearance."
- Red Light (500s): "The filing cabinet is broken. It's my fault, not yours."
4. The Core HTTP Methods (The Verbs)
In REST API design, we use HTTP Methods to indicate our intention to the server.- 1. GET (Read): Retrieves data from the server.
-
*Example:*
GET /api/users(Fetch a list of users).
- *Rule:* GET requests must be Idempotent and Safe. They should *never* alter data in the database.
- 2. POST (Create): Submits new data to the server to create a new resource.
-
*Example:*
POST /api/users(Create a new user account).
- *Rule:* POST is NOT idempotent. If you send the same POST request 3 times, you will create 3 identical users.
- 3. PUT (Update/Replace): Replaces an entire existing resource with new data.
-
*Example:*
PUT /api/users/1(Replace user #1's profile).
- *Rule:* PUT *is* idempotent. Sending the exact same update request 10 times results in the exact same database state as sending it once.
- 4. DELETE (Delete): Removes a resource.
-
*Example:*
DELETE /api/users/1(Delete user #1).
- 5. *(Bonus)* PATCH (Partial Update): Updates only specific fields of a resource (e.g., just changing the email, leaving the rest untouched).
5. HTTP Status Codes (The Vocabulary)
When an API responds, the most important part of the response isn't the JSON data; it is the 3-digit Status Code in the HTTP Header. They are divided into 5 categories based on their first digit:- 1xx (Informational): Rarely used in standard REST APIs.
- 2xx (Success): Everything worked perfectly.
- 3xx (Redirection): The resource moved; go look over there.
- 4xx (Client Error): The Client (Frontend) messed up.
- 5xx (Server Error): The Server (Backend) crashed.
6. The Most Important Codes to Memorize
You must use these codes correctly when building your API:The Successes (200s)
- 200 OK: Standard success for GET and PUT.
- 201 Created: Specific success for POST. "I successfully created the database record."
- 204 No Content: Success for DELETE. "I deleted it, and I have no data to send back to you."
The Client Errors (400s) - Your Fault
- 400 Bad Request: You sent invalid JSON or missing fields.
- 401 Unauthorized: You are not logged in. (Missing or invalid token).
- 403 Forbidden: You are logged in, but you don't have Admin permissions to do this.
- 404 Not Found: The URL or Resource ID does not exist.
The Server Errors (500s) - My Fault
- 500 Internal Server Error: My database crashed or my code threw an exception.
7. Real-World API Examples
Let's look at how the Verb and the Noun combine.
http
8. Implementation Examples (Express & Laravel)
Notice how frameworks explicitly map the HTTP Method to the logic.Express.js (Node)
javascript
Laravel (PHP)
php
9. Best Practices
-
Never Return 200 for Errors: A massive anti-pattern is an API that crashes, but the server returns HTTP
200 OKwith a JSON body saying{ "status": "error" }. This breaks frontend networking libraries (like Axios or Fetch) which rely on the 3-digit HTTP code to trigger.catch()blocks. If there is an error, return a 4xx or 5xx code!
10. Common Mistakes
-
Using GET to Delete or Create: You should never design a URL like
GET /api/users/delete/5. Because browsers pre-fetch GET requests to speed up loading, a browser might accidentally navigate to that URL in the background, deleting your database records unintentionally! GET must remain strictly read-only.
11. Exercises
- 1. Map the four primary HTTP Methods (GET, POST, PUT, DELETE) to the four database CRUD operations (Create, Read, Update, Delete).
12. Coding Challenges
- Challenge: You are designing an API endpoint that allows a user to update their email address. They submit an email that is already taken by another user. Which specific 4xx Status Code should your API return to indicate the Client made a bad request?
13. MCQs with Answers
Question 1
According to strict REST principles, which HTTP Method should be utilized when a client wants to submit a payload to create a brand new resource in the database?
Question 2
An API client attempts to access the endpoint /api/admin/dashboard. The server verifies the client's token, confirms the client is successfully logged in, but determines the client holds a "standard" role rather than an "admin" role. Which HTTP Status Code MUST the server return?
14. Interview Questions
- Q: Explain the concept of "Idempotency" in REST APIs. Which HTTP Methods are idempotent, and which are not?
-
Q: Walk me through the difference between a
401 Unauthorizedand a403 Forbiddenstatus code. Provide a concrete example of when an API should return each.
15. FAQs
Q: What is the difference between PUT and PATCH? A:PUT replaces the *entire* object. If a user has a name, email, and age, and you only send { "age": 30 } via PUT, the server should delete the name and email. PATCH is for partial updates. If you send { "age": 30 } via PATCH, the server updates only the age and leaves the name and email alone.