HTTP methods
HTTP Methods are types of requests a client (like a browser or mobile app) can make to a server to perform different actions on resources (like data or files).
Common HTTP Methods
| Method | Purpose |
|---|---|
| GET | Used to read/retrieve data from a web server. |
| POST | Used to send data (files, form data, etc.) to the server. |
| PUT | Used to update the data on the server. |
| PATCH | Used to update the part of the data on the server. |
| DELETE | Used to delete the data on the server. |
The Get Method
The GET method is one of the most commonly used HTTP methods. It retrieves data from the server without modifying it.
Key Points:
- GET requests can be cached.
- GET requests remain in the browser history.
- GET requests can be bookmarked.
- GET requests should never be used when dealing with sensitive data.
- GET requests have length restrictions.
- GET requests are only used to request data (not modify).
Note: Never send sensitive data (like passwords or tokens) in the URL using GET — it's stored in browser history and logs.
The Post Method
The POST method is used to send data to the server to create a new resource. Unlike GET, it modifies data on the server and typically includes a request body.
Key Points:
- POST requests are never cached.
- POST requests do not remain in the browser history.
- POST requests cannot be bookmarked.
- POST requests have no restrictions on data length.
The Put Method
The PUT method is used to update an entire data on the server. It replaces the existing resource with the new data sent in the request body.
Key Points:
- PUT is primarily used to update an existing resource completely.
- It replaces all fields of the resource. If a field is missing in the request body, it may be removed or set to null.
- The full data for the resource must be sent in the request body.
- It modifies data on the server, so it’s not considered a "safe" HTTP method.
The Patch Method
The PATCH method is used to partially update a resource. Unlike PUT, PATCH updates only the specific fields provided in the request.
Key Points:
- Used to update only specific fields of a resource.
- You don't need to send the full object.
- It is not safe, meaning it changes data on the server.
- Contains only the fields you want to change (e.g., just a name or status).
- Preferred when updating a single property of a resource (e.g., change only the email address).
- Since you're sending less data, it can be more network-efficient compared to PUT.
The Delete Method
The DELETE method is used to delete a resource from the server.
Key Points:
- Removes a specific resource identified by the URI.
- It changes server data (i.e., deletes it).
- Multiple DELETE requests for the same resource have the same effect—once it's deleted, it's gone. Sending DELETE again doesn’t throw an error; the resource just remains deleted.
- Contains only the fields you want to change (e.g., just a name or status).
- Usually doesn't require a request body (but some APIs accept it with caution).
- Since you're sending less data, it can be more network-efficient compared to PUT.