1.HTTP Methods
HTTP Methods
- GET
- POST
- PUT
- HEAD
- DELETE
- PATCH
- OPTIONS
GET is used to request data from a specified resource.
GET is one of the most common HTTP methods.
POST is used to send data to a server to create/update a resource.
POST is one of the most common HTTP methods.
PUT is used to send data to a server to create/update a resource.
The difference between POST and PUT is that PUT requests are idempotent. That is, calling the same PUT request multiple times will always produce the same result. In contrast, calling a POST request repeatedly have side effects of creating the same resource multiple times.
POST和PUT之間的區別在於PUT請求是冪等的。 也就是說,多次調用相同的PUT請求將始終產生相同的結果。 相反,重復調用POST請求具有多次創建相同資源的副作用。
2.冪等性
冪等是一個數學與計算機學概念,常見於抽象代數中。
冪等是使公式f(x)=f(f(x))
能夠成立的數學性質。
相關閱讀:冪等 - 術語表 | MDN
3.POST和PUT的區別
POST和PUT之間的區別在於PUT請求是冪等的。
GET /tickets # 獲取ticket列表
POST /tickets # 新建一個ticket
PUT /tickets/1 # 更新ticket 1
DELETE /tickets/1 # 刪除ticekt 1
多次調用GET /tickets
和第一次調用GET /tickets
對資源的影響是一致的。
多次調用PUT /tickets/1
和第一次調用PUT /tickets/1
對資源的影響是一致的。
多次調用DELETE /tickets/1
和第一次調用DELETE /tickets/1
對資源的影響是一致的。
多次調用POST /tickets
都將產生新的資源(對資源的影響是不一致的)。
Tips:我把這里的資源理解為數據庫中的數據。
因此,GET、PUT、和DELETE方法都是冪等的,而POST方法不是冪等的。
事實上,POST請求和PUT請求可能只存在協議上的區別。
相關閱讀:HTTP中post和put的根本區別和優勢? - 知乎