它们似乎都在向身体内部的服务器发送数据,那么它们有什么不同呢?
当前回答
POST被认为是某种工厂类型的方法。您将数据包含在其中以创建您想要的内容,而另一端的任何内容都知道如何使用它。PUT用于更新给定URL上的现有数据,或者当您知道URI将是什么并且它还不存在时创建一些新的东西(与POST相反,POST将创建一些东西并在必要时返回URL)。
其他回答
HTTP PUT:
PUT将一个文件或资源放在一个特定的URI中,并且正是在这个URI中。如果该URI上已经有文件或资源,PUT将替换该文件或资源。如果没有文件或资源,PUT会创建一个。PUT是幂等的,但矛盾的是PUT响应是不可缓存的。
PUT的HTTP 1.1 RFC位置
HTTP POST:
POST将数据发送到特定的URI,并期望该URI上的资源处理请求。此时,web服务器可以确定在指定资源的上下文中如何处理数据。POST方法不是幂等的,但是POST响应是可缓存的,只要服务器设置了适当的Cache-Control和Expires头。
正式的HTTP RFC指定POST为:
现有资源的注释; 在公告栏、新闻组、邮件列表上发布消息, 或类似的文章组; 提供一个数据块,例如提交的结果 表单,来一个数据处理过程; 通过追加操作扩展数据库。
POST的HTTP 1.1 RFC位置
POST和PUT的区别:
RFC本身解释了核心差异:
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. If the server desires that the request be applied to a different URI, it MUST send a 301 (Moved Permanently) response; the user agent MAY then make its own decision regarding whether or not to redirect the request.
此外,更简单地说,RFC 7231章节4.3.4 PUT声明(强调添加),
4.3.4. 把 PUT方法请求目标资源的状态为 用表示形式定义的状态创建或替换 包含在请求消息有效负载中。
使用正确的方法,抛开无关:
REST ROA相对于SOAP的一个好处是,当使用HTTP REST ROA时,它鼓励正确使用HTTP动词/方法。因此,例如,当您想要在该确切位置创建资源时,才会使用PUT。而且您永远不会使用GET来创建或修改资源。
GET: Retrieves data from the server. Should have no other effect. PUT: Replaces target resource with the request payload. Can be used to update or create a new resource. PATCH: Similar to PUT, but used to update only certain fields within an existing resource. POST: Performs resource-specific processing on the payload. Can be used for different actions including creating a new resource, uploading a file, or submitting a web form. DELETE: Removes data from the server. TRACE: Provides a way to test what the server receives. It simply returns what was sent. OPTIONS: Allows a client to get information about the request methods supported by a service. The relevant response header is Allow with supported methods. Also used in CORS as preflight request to inform the server about actual the request method and ask about custom headers. HEAD: Returns only the response headers. CONNECT: Used by the browser when it knows it talks to a proxy and the final URI begins with https://. The intent of CONNECT is to allow end-to-end encrypted TLS sessions, so the data is unreadable to a proxy.
REST-ful用法
POST用于创建一个新资源,然后返回资源URI
EX
REQUEST : POST ..../books
{
"book":"booName",
"author":"authorName"
}
这个调用可以创建一本新书,并返回该书籍的URI
Response ...THE-NEW-RESOURCE-URI/books/5
PUT是用来替换资源的,如果资源存在,就更新它,如果资源不存在,就创建它,
REQUEST : PUT ..../books/5
{
"book":"booName",
"author":"authorName"
}
使用PUT,我们知道资源标识符,但POST将返回新的资源标识符
无剩余用途
POST用于在服务器端发起一个操作,这个操作可能会创建资源,也可能不会创建资源,但是这个操作会有副作用,它总是会改变服务器上的一些东西
PUT用于放置或替换特定URL上的文字内容
REST-ful和non - rest风格的另一个区别
POST是非幂等操作:如果对同一个请求执行多次,将导致一些变化。
PUT是幂等操作:如果对同一个请求执行多次,它将没有副作用。
总结
使用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.”
值得一提的是,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,在发出实际请求之前需要进一步请求服务器的许可。]所谓的飞行前请求[..]
推荐文章
- HTTP 1.1和HTTP 2.0的区别是什么?
- 什么是“升级-不安全-请求”HTTP报头?
- 我如何捕捉Ajax查询后错误?
- HTTP 301和308状态码有什么区别?
- 什么HTTP状态码应该用于错误的输入
- application/json和application/x-www-form-urlencoded有什么区别?
- 编排microservices
- 如何使HTTP请求在PHP和不等待响应
- PATCH和PUT请求的主要区别是什么?
- 我可以把我所有的http://链接都改成//吗?
- URL为AJAX请求编码一个jQuery字符串
- 编译System.Net.HttpClient的查询字符串
- 摘要认证和基本认证的区别是什么?
- Axios -删除请求与请求体和头?
- 如何在http获取请求设置报头?