它们似乎都在向身体内部的服务器发送数据,那么它们有什么不同呢?
当前回答
总结
使用PUT创建或用请求中包含的表示定义的状态替换目标资源的状态。这种标准化的预期效果是幂等的,因此它通知中介,在通信失败的情况下,他们可以重复请求。 否则使用POST(包括创建或替换目标资源以外的资源状态)。其预期效果不规范,中介机构不能依赖任何普遍属性。
参考文献
关于POST和PUT请求方法之间语义差异的最新权威描述在RFC 7231中给出(Roy Fielding, Julian Reschke, 2014):
The fundamental difference between the POST and PUT methods is highlighted by the different intent for the enclosed representation. The target resource in a POST request is intended to handle the enclosed representation according to the resource's own semantics, whereas the enclosed representation in a PUT request is defined as replacing the state of the target resource. Hence, the intent of PUT is idempotent and visible to intermediaries, even though the exact effect is only known by the origin server.
换句话说,PUT的预期效果是标准化的(用请求中所包含的表示定义的状态创建或替换目标资源的状态),因此对所有目标资源都是通用的,而POST的预期效果不是标准化的,因此是特定于每个目标资源的。因此POST可以用于任何事情,包括实现PUT和其他请求方法(GET、HEAD、DELETE、CONNECT、OPTIONS和TRACE)的预期效果。
But it is recommended to always use the more specialized request method rather than POST when applicable because it provides more information to intermediaries for automating information retrieval (since GET, HEAD, OPTIONS, and TRACE are defined as safe), handling communication failure (since GET, HEAD, PUT, DELETE, OPTIONS, and TRACE are defined as idempotent), and optimizing cache performance (since GET and HEAD are defined as cacheable), as explained in It Is Okay to Use POST (Roy Fielding, 2009):
POST only becomes an issue when it is used in a situation for which some other method is ideally suited: e.g., retrieval of information that should be a representation of some resource (GET), complete replacement of a representation (PUT), or any of the other standardized methods that tell intermediaries something more valuable than “this may change something.” The other methods are more valuable to intermediaries because they say something about how failures can be automatically handled and how intermediate caches can optimize their behavior. POST does not have those characteristics, but that doesn’t mean we can live without it. POST serves many useful purposes in HTTP, including the general purpose of “this action isn’t worth standardizing.”
其他回答
据我所知,PUT主要用于更新记录。
POST -创建文档或任何其他资源 PUT—更新创建的文档或任何其他资源。
但要清楚的是,PUT通常“替换”现有的记录,如果它在那里,如果它不在那里创建。
请参阅:http://zacharyvoase.com/2009/07/03/http-post-put-diff/
最近,我对web开发人员的一个流行误解感到非常恼火,他们认为POST是用来创建资源的,而PUT是用来更新/更改资源的。
如果你看一下RFC 2616的第55页(“超文本传输协议- HTTP/1.1”),第9.6节(“PUT”),你会看到PUT实际上是干什么的:
PUT方法要求将所包含的实体存储在所提供的Request-URI下。
还有一个方便的段落解释了POST和PUT之间的区别:
The fundamental difference between the POST and PUT requests is reflected in the different meaning of the Request-URI. The URI in a POST request identifies the resource that will handle the enclosed entity. That resource might be a data-accepting process, a gateway to some other protocol, or a separate entity that accepts annotations. In contrast, the URI in a PUT request identifies the entity enclosed with the request – the user agent knows what URI is intended and the server MUST NOT attempt to apply the request to some other resource.
它没有提到更新/创建之间的区别,因为这不是它的重点。这是关于这两者之间的区别:
obj.set_attribute(value) # A POST request.
这:
obj.attribute = value # A PUT request.
所以,请停止这种流行的误解的传播。阅读rfc。
简单来说,你可以说:
1.HTTP Get:用于获取一个或多个条目
2.HTTP Post:用于创建条目
3.HTTP Put:用于更新条目
4.HTTP补丁:用于部分更新项目
5.HTTP删除:用于删除项目
值得一提的是,POST容易受到一些常见的跨站请求伪造(CSRF)攻击,而PUT则不会。
当受害者访问attackersite.com时,下面的CSRF是不可能的。
攻击的效果是,受害者无意中删除一个用户,只是因为它(受害者)在访问attackersite.com之前以admin身份登录target.site.com:
attackersite.com上的恶意代码:
案例1:正常请求。保存的target.site.com cookie将自动由浏览器发送:(注意:只支持PUT,在端点,是更安全的,因为它是不支持<form>属性值)
<!--deletes user with id 5-->
<form id="myform" method="post" action="http://target.site.com/deleteUser" >
<input type="hidden" name="userId" value="5">
</form>
<script>document.createElement('form').submit.call(document.getElementById('myform'));</script>
案例2:XHR请求。保存的target.site.com cookie将被浏览器自动发送:(注意:只支持PUT,在端点,是更安全的,因为尝试发送PUT将触发一个preflight请求,其响应将阻止浏览器请求deleteUser页面)
//deletes user with id 5
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://target.site.com/deleteUser");
xhr.withCredentials=true;
xhr.send(["userId=5"]);
MDN Ref: [..]与“简单请求”(上面讨论过)不同,——[[意思是:POST/GET/HEAD]]——,对于“预飞行”请求,浏览器首先使用OPTIONS方法发送一个HTTP请求[..]
Cors在行动:[..]某些类型的请求,如DELETE或PUT,在发出实际请求之前需要进一步请求服务器的许可。]所谓的飞行前请求[..]
POST被认为是某种工厂类型的方法。您将数据包含在其中以创建您想要的内容,而另一端的任何内容都知道如何使用它。PUT用于更新给定URL上的现有数据,或者当您知道URI将是什么并且它还不存在时创建一些新的东西(与POST相反,POST将创建一些东西并在必要时返回URL)。
推荐文章
- 什么是HTTP中的“406-不可接受的响应”?
- 最好的轻量级web服务器(只有静态内容)的Windows
- HTTP POST在Java中使用JSON
- 如何防止页面刷新时重新提交表单(F5 / CTRL+R)
- 哪些HTTP方法与哪些CRUD方法相匹配?
- 使用HTML形式的PUT方法
- 如何转义哈希字符在URL
- 有没有办法在python中做HTTP PUT
- 我能在服务器端应用程序(PHP、Ruby、Python等)上读取URL的哈希部分吗?
- 什么是http头“X-XSS-Protection”?
- 在nodejs http中body在哪里。得到回应?
- HTTP请求在Swift与POST方法
- Ruby:如何将散列转换为HTTP参数?
- 在用nodejs和express创建的REST API中设置响应状态和JSON内容的正确方法
- HTTP 1.1和HTTP 2.0的区别是什么?