Restful Api Http Method
## HTTP Methods
HTTP methods are like "action instructions" for your data.
In RESTful API, we mainly use four methods, which correspond to four basic operations on data:
| HTTP Method | Action | Corresponding Operation | Real-life Analogy |
| --- | --- | --- | --- |
| GET | Get data | Read | View menu |
| POST | Create data | Create | Order food |
| PUT | Update data | Update | Modify order |
| DELETE | Delete data | Delete | Cancel order |
### GET Method
GET is used to get data, just like asking the waiter "What dishes do you have today?"
// Get all users
GET /api/users
// Get specific user
GET /api/users/123
// Get user's orders
GET /api/users/123/orders
**Characteristics**:
* Safe operation, does not modify data
* Can be cached
* Parameters are in the URL
### POST Method
POST is used to create new data, just like telling the waiter "I want to order Kung Pao Chicken"
// Create new user
POST /api/users
{
"name": "Zhang San",
"email": "zhangsan@example.com"
}
// Create new order
POST /api/orders
{
"userId": 123,
"items": ["Item A", "Item B"]
}
**Characteristics**:
* Modifies server state
* Not idempotent (multiple calls will create multiple resources)
* Data is in the request body
### PUT Method
PUT is used to update the entire resource, just like saying "Change my order to this"
// Update user information
PUT /api/users/123
{
"name": "Li Si",
"email": "lisi@example.com",
"phone": "13800138000"
}
**Characteristics**:
* Idempotent (multiple calls produce the same result)
* Usually replaces the entire resource
* If the resource doesn't exist, it may create a new resource
### DELETE Method
DELETE is used to delete resource, just like saying "Cancel my order"
// Delete user
DELETE /api/users/123
// Delete order
DELETE /api/orders/456
* Idempotent
* Deletes the
YouTip