Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTTP methods

HTTP methods

An HTTP method is like an "action command" you give to the data.

In a RESTful API, we mainly use four methods, which correspond to the four basic operations on data:

HTTP methods Purpose Corresponding operation Real-life analogy
GET Get the data Read View the menu
POST Create data creation Place an order
PUT Update data update Modify the order
DELETE Delete data Delete Cancel the order

GET Method

GET is used to get data, like asking the waiter "what dishes are available today?"

// 获取所有用户

GET /api/users



// Get specific user

GET /api/users/123



// Get user's orders

GET /api/users/123/orders

Feature:

POST Method

POST is used to create new data, like telling the waiter "I want to order a Kung Pao chicken"



// Create new user

POST /api/users

{

  "name": "张三",

  "email": "zhangsan@example.com"

}



// Create new order

POST /api/orders

{

  "userId": 123,

  "items": ["商品A", "商品B"]

}

Feature:

The PUT method

PUT is used to update an entire resource, like saying "change my order to this"



// Update user information

PUT /api/users/123

{

  "name": "李四",

  "email": "lisi@example.com",

  "phone": "13800138000"

}

Feature:

The DELETE method

DELETE is used to delete a resource, like saying "cancel my order"

// 删除用户

DELETE /api/users/123



// Delete order

DELETE /api/orders/456

HTTP Status Codes

After the server processes the request, it returns a status code telling you the result:

Status code Meaning Description
200 OK Request succeeded
201 Created Resource created successfully
400 Bad Request Bad request
401 Unauthorized Unauthorized
404 Not Found Resource not found
500 Internal Server Error Internal server error