HTTP Methods
HTTP Methods
An HTTP method is a way for a client (usually a web browser or API client) to communicate with a server using the HTTP (Hypertext Transfer Protocol).
An HTTP method (also called an HTTP verb) defines the action that a client wants to perform on a resource identified by a URL when making an HTTP request. HTTP methods define the type of action the client wants to perform on a resource, such as retrieving, creating, updating, or deleting data.
These methods are part of the HTTP protocol, which is the foundation of data communication on the web. These methods are crucial for REST APIs and web development.
The most common HTTP Methods are as follows:
- GET
- POST
- PUT
- DELETE
GET Method
Purpose: Retrieves data from a server without altering it.
Use Case: Fetching user details, product lists, or blog posts.
Characteristics:
- Non-destructive (read-only).
- Can be cached by browsers.
- Query parameters are appended to the URL.
Example:
API Endpoint: https://restapi.example.com/users Result: Returns a list of users.
In Postman tool, you can select the HTTP method from the drop-down menu.
POST Method
Purpose: Sends data to the server to create a new resource.
Use Case: Creating a new user account, submitting a form, or uploading a file.
Characteristics:
- Data is sent in the request body.
- Typically results in a change on the server.
- Not idempotent (sending the same request multiple times creates multiple resources).
Example:
API Endpoint: https://restapi.example.com/users Request Body: { "name": "John Doe", "email": "[email protected]" } Result: A new user is created.
PUT Method
Purpose: Updates an existing resource or creates one if it does not exist.
Use Case: Updating user information or replacing an existing resource.
Characteristics:
- Data is sent in the request body.
- Idempotent (sending the same request multiple times results in the same state).
Example:
API Endpoint: https://restapi.example.com/users/1 Request Body: { "name": "John Updated", "email": "[email protected]" } Result: Updates the user with ID 1.
DELETE Method
Purpose: Removes a resource from the server.
Use Case: Deleting a user account or a specific record.
Characteristics:
- Non-reversible.
- Typically idempotent (multiple delete requests for the same resource have the same effect).
Example:
API Endpoint: https://restapi.example.com/users/1 Result: Deletes the user with ID 1.
HTTP Methods Comparison
Method | Purpose | Idempotent | Data Location | Use Case |
---|---|---|---|---|
GET | Retrieve data | Yes | URL/Query params | Fetching data |
POST | Create new resource | No | Request body | Creating new records |
PUT | Update or replace resource | Yes | Request body | Updating existing records |
DELETE | Remove resource | Yes | URL/Query params | Deleting data |