幂等性(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的put delete get post
Django 中request.get和request.post的区别 POST和GET差异: POST和GET是HTTP协议定义的与服务器交互的方法。GET一般用于获取/查询资源信息,而POST一般用于更新资源信息。另外,还有PUT和DELETE方法。 POST和GET都...
是一个Java 发送http put、delete、 post、 get 请求的工具类。可用在与restful service 进行通讯的代码中。
摘 要:浅谈HTTP中GET、POST用法以及它们的区别 关键词: HTTP协议、POST方法、GET方法、区别 HTTP方法 HTTP定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE。URL全称是资源描述符...
http接口通用调试工具,支持get、post、put、patch、delete方法,数据的发送和接收使用json格式,即Content-Type: application/json
Qt http同步请求测试(restfull中常用的get/post/put/delete、文件上传/下载) 示例源码 https://blog.csdn.net/aggs1990/article/details/124104548 CSDN审核可能较慢,如无法下载,可以过段时间再回来看下
https支持put get post请求经测式可以行得通,不需要密匙
UniApp中封装request网络请求包含GET、POST、PUT、DELETE等,更加方便实现网络数据请求,包含项目的基本框架目录,可直接用于项目开发,适用于app和微信小程序等项目
主要实现HTTP POST/GET/PUT/DELETE等基本操作方式,此外还实现了代理、Header读取、HTML解析特定标签的属性值。
测试接口,模拟POST,GET,PUT,DELETE提交的工具
post_get_put等请求方法有什么不同
超文本传输协议(HTTP)的...两种 HTTP 请求方法:GET 和 POST 在客户机和服务器之间进行请求-响应时,两种最常被用到的方法是:GET 和 POST。 GET - 从指定的资源请求数据。 POST - 向指定的资源提交要被处理的数据
包含post、delete、put、get方法四个restful支持的服务,客户端向服务端发出的post等四个操作的http头,并携带参数向服务端获取响应。 实现了book的增删改查
这是一个关于get,put相关介绍,希望大家能够有所收获,谢谢大家
服务端模拟ajax,支持发起GET、POST、PUT、DELETE请求
URL全称是资源描述符,我们可以这样认为:一个URL地址,它用于描述一个网络上的资源,而HTTP中的GET,POST,PUT,DELETE就对应着对这个资源的查,改,增,删4个操作。到这里,大家应该有个大概的了解了,GET一般用于...
对http的get、post、put 方法进行封装,置成lib直接调用
C#(cs文件) http连接api连接基础连接实例GET POST PUT DELETE
HttpClient工具类,简单轻松的实现get,post,put和delete请求