PUT和POST的区别

Post vs Put

Post:创建,不安全,不幂等

Put:更新或创建,幂等

比较容易混淆的是HTTP POST和PUT。POST和PUT的区别容易被简单地误认为“POST表示创建资源,PUT表示更新资源”;而实际上,二者均可用于创建资源,更为本质的差别是在幂等性方面。在HTTP规范中对POST和PUT是这样定义的:

The POST method isused to request that the origin server accept the entity enclosed in 
therequest as a new subordinate of the resource identified by the Request-URI inthe 
Request-Line ...... If a resource has been created on the origin server,the response 
SHOULD be 201 (Created) and contain an entity which describes thestatus of 
the request and refers to the new resource, and a Location header.

The PUT methodrequests that the enclosed entity be stored under the supplied Request-URI. 
If the Request-URI refers to an already existing resource, 
the enclosed entitySHOULD be considered as a modified version of the one residing on the originserver. 
If the Request-URI does not point to an existing resource,
and that URIis capable of being defined as a new resource by the requesting user agent, 
the origin server can create the resource with that URI

POST所对应的URI并非创建的资源本身,而是资源的接收者。比如:POST 的语义是在http://www.forum.com/articles下创建一篇帖子,HTTP响应中应包含帖子的创建状态以及帖子的URI。两次相同的POST请求会在服务器端创建两份资源,它们具有不同的URI;所以,POST方法不具备幂等性。

而PUT所对应的URI是要创建或更新的资源本身。比如:PUT http://www.forum/articles/4231的语义是创建或更新ID为4231的帖子。对同一URI进行多次PUT的副作用和一次PUT是相同的;因此,PUT方法具有幂等性。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值