http中put和post区别

幂等性(Idempotency):(分布式系统的特性)幂等性是数学中的一个概念,表达的是N次变换与1次变换的结果相同,

定义如下:

单目运算, x为某集合内的任意数, f为运算子如果满足f(x)=f(f(x)), 那么我们称f运算为具有幂等性(idempotent)

比如在实数集中,绝对值运算就是一个例子: abs(a)=abs(abs(a))

双目运算,x为某集合内的任意数, f为运算子如果满足f(x,x)=x, f运算的前提是两个参数都同为x, 那么我们也称f运算为具有幂等性

比如在实数集中,求两个数的最大值的函数: max(x,x) = x, 还有布尔代数中,逻辑运算 "与", "或" 也都是幂等运算, 因为他们符合AND(0,0) = 0, AND(1,1) = 1, OR(0,0) = 0, OR(1,1) = 1。

 

在计算机中,我们可以通过get,delete,put,post理解:

HTTP GET方法用于获取资源,不应有副作用,所以是幂等的。比如:GET http://www.bank.com/account/123456,不会改变资源的状态,不论调用一次还是N次都没有副作用。请注意,这里强调的是一次和N次具有相同的副作用,而不是每次GET的结果相同。GET http://www.news.com/latest-news这个HTTP请求可能会每次得到不同的结果,但它本身并没有产生任何副作用,因而是满足幂等性的。

 

HTTP DELETE方法用于删除资源,有副作用,但它应该满足幂等性。比如:DELETE http://www.forum.com/article/4231,调用一次和N次对系统产生的副作用是相同的,即删掉id为4231的帖子;因此,调用者可以多次调用或刷新页面而不必担心引起错误。

 

比较容易混淆的是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. Ifthe 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, theorigin server can create the resource with that URI.

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

 

For instance:比如浏览一个博客,无论浏览多少次,都不会读这个博客造成影响,也许会说增加了访问量,但是这个不是因为你浏览帖子造成的,造成改变的是该博客本身的逻辑设计,也许你会说,这个博客内容让更新或者删除了,得到的浏览结果不一样了,得到的是一样的,里面的内容不是因为你的操作造成的,而是因为博主或者其他原因。

 

基于幂等性处理的问题:取钱问题,进而引入发帖问题。

 

Post vs Put

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

Put:更新或创建,幂等

 

About creat:POST 是作用在一个集合资源之上的(/articles),而PUT操作是作用在一个具体资源之上的(/articles/123),再通俗点说,如果URL可以在客户端确定,那么就使用PUT,如果是在服务端确定,那么就使用POST,比如说很多资源使用数据库自增主键作为标识信息,而创建的资源的标识信息到底是什么只能由服务端提供,这个时候就必须使用POST。

       Post:Use POST to create resources when you do not know the resourceidentifier. With POST creates, it is best practice to return the status of “201Created” and the location of the newly created resource, since its location wasunknown at the time of submission.  Thisallows the client to access the new resource later if they need to.

       Put:Use PUT when you allow the client to specify the resourceidentifier of the newly created resource. But remember, since PUT is idempotent, you must send all possiblevalues.

 

About update:

       You can use POST to send either allavailable values or just a subset of available values;

       If you want to use PUT to update aresource, it must be a full resource update; you MUST send all attribute valuesin a PUT request to guarantee idempotency. Use PUT when you want or need to send all available values in order tofollow idempotency requirements, for instance in the case of a full resourceupdate.

       You can also use POST to send all valuesas well and the server state could be the same as a full PUT – it’s just notrequired to be by the HTTP specification. Note that idempotency has a strong correlationto being cacheable by HTTP caching servers, and therefore POST requests aregenerally not cached. If you are ok with this caching side effect, you can usePOST for both full and partial updates.

你可能感兴趣的:(http)