HTTP Methods define the type of action a client (browser or app) wants to perform on a server.
Whenever you interact with a website or API, HTTP methods are used behind the scenes to send requests and receive responses.
HTTP stands for HyperText Transfer Protocol â the communication protocol between client and server.
Why HTTP Methods Are Important
HTTP methods tell the server what to do with the requested resource.
For example:
- Retrieve data
- Send new data
- Update existing data
- Delete data
Each method has a specific purpose.
1. GET
GET is used to retrieve data from the server.
It does not modify data.
Example:
GET /users
GET /products/1
Use cases:
- Viewing a webpage
- Fetching user details
- Loading product listings
GET requests:
- Data is sent in the URL
- Can be cached
- Should not change server data
2. POST
POST is used to send new data to the server.
It is commonly used to create resources.
Example:
POST /users
Use cases:
- Submitting a registration form
- Creating a new account
- Uploading data
POST requests:
- Data is sent in the request body
- Not cached by default
- Can change server data
3. PUT
PUT is used to update existing data.
It replaces the entire resource.
Example:
PUT /users/1
Use cases:
- Updating profile information
- Modifying product details
PUT requests:
- Sent in request body
- Replaces full resource
4. PATCH
PATCH is used to partially update data.
Unlike PUT, it updates only specific fields.
Example:
PATCH /users/1
Use cases:
- Updating only email address
- Changing password
5. DELETE
DELETE is used to remove data from the server.
Example:
DELETE /users/1
Use cases:
- Deleting user account
- Removing product from database
Safe vs Unsafe Methods
Safe methods (do not change server data):
- GET
Unsafe methods (modify data):
- POST
- PUT
- PATCH
- DELETE
Idempotent Methods
An idempotent method produces the same result no matter how many times it is repeated.
Idempotent:
- GET
- PUT
- DELETE
Not idempotent:
- POST
Real-World Example
Login process:
POST /login â Send username and password
Viewing profile:
GET /profile
Updating profile:
PUT /profile
Deleting account:
DELETE /account
Key Takeaway
HTTP Methods define the action performed on a server resource.
GET retrieves data.
POST creates data.
PUT updates data.
PATCH partially updates data.
DELETE removes data.
Understanding HTTP methods is essential for working with web applications and APIs.