首页 > Web开发 > 详细

理解HTTP的POST和PUT的区别

时间:2020-04-04 22:44:07      阅读:62      评论:0      收藏:0      [点我收藏+]

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:我把这里的资源理解为数据库中的数据。

因此,GETPUT、和DELETE方法都是幂等的,而POST方法不是幂等的。

事实上,POST请求和PUT请求可能只存在协议上的区别。
相关阅读:HTTP中post和put的根本区别和优势? - 知乎

理解HTTP的POST和PUT的区别

原文:https://www.cnblogs.com/gzhjj/p/12633904.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!