PUT和POST的区别
Post vs PutPost:创建,不安全,不幂等Put:更新或创建,幂等比较容易混淆的是HTTP POST和PUT。POST和PUT的区别容易被简单地误认为“POST表示创建资源,PUT表示更新资源”;而实际上,二者均可用于创建资源,更为本质的差别是在幂等性方面。在HTTP规范中对POST和PUT是这样定义的:The POST method isused to request that the
·
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条内容
所有评论(0)