它们似乎都在向身体内部的服务器发送数据,那么它们有什么不同呢?


当前回答

PUT是一种将内容“上载”到特定URI或覆盖该URI中已有内容的方法。

另一方面,POST是提交与给定URI相关的数据的一种方式。

参考HTTP RFC

其他回答

根据HTTP方法定义操作

HTTP协议定义了许多为请求分配语义的方法。大多数RESTful web api使用的常见HTTP方法有:

GET在指定URI处检索资源的表示形式。响应消息的主体包含所请求资源的详细信息。

POST在指定的URI上创建一个新资源。请求消息的主体提供了新资源的详细信息。注意,POST还可以用于触发不实际创建资源的操作。

PUT在指定的URI上创建或替换资源。请求消息的主体指定要创建或更新的资源。

PATCH执行资源的部分更新。请求体指定要应用于资源的更改集。

DELETE删除指定URI上的资源。

特定请求的效果应取决于资源是集合还是单个项。下表通过电子商务示例总结了大多数RESTful实现采用的常见约定。并不是所有这些请求都可以实现——这取决于具体的场景。

Resource POST GET PUT DELETE
/customers Create a new customer Retrieve all customers Bulk update of customers Remove all customers
/customers/1 Error Retrieve the details for customer 1 Update the details of customer 1 if it exists Remove customer 1
/customers/1/orders Create a new order for customer 1 Retrieve all orders for customer 1 Bulk update of orders for customer 1 Remove all orders for customer 1

POST、PUT和PATCH之间的区别可能令人困惑。

POST请求创建一个资源。服务器为新资源分配URI,并将该URI返回给客户端。在REST模型中,您经常对集合应用POST请求。新资源被添加到集合中。POST请求还可以用于将数据提交到现有资源进行处理,而不需要创建任何新资源。

A PUT request creates a resource or updates an existing resource. The client specifies the URI for the resource. The request body contains a complete representation of the resource. If a resource with this URI already exists, it is replaced. Otherwise, a new resource is created, if the server supports doing so. PUT requests are most frequently applied to resources that are individual items, such as a specific customer, rather than collections. A server might support updates but not creation via PUT. Whether to support creation via PUT depends on whether the client can meaningfully assign a URI to a resource before it exists. If not, then use POST to create resources and PUT or PATCH to update.

PATCH请求对现有资源执行部分更新。客户端为资源指定URI。请求体指定要应用于资源的一组更改。这可能比使用PUT更有效,因为客户端只发送更改,而不是资源的整个表示。从技术上讲,如果服务器支持的话,PATCH还可以创建一个新资源(通过指定一组对“null”资源的更新)。

PUT请求必须是等幂的。如果客户端多次提交相同的PUT请求,结果应该总是相同的(相同的资源将被修改为相同的值)。POST和PATCH请求不能保证是等幂的。

请参阅: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。

Post和Put主要用于发布数据和其他更新数据。但是你可以只对post request做同样的事情。

POST被认为是某种工厂类型的方法。您将数据包含在其中以创建您想要的内容,而另一端的任何内容都知道如何使用它。PUT用于更新给定URL上的现有数据,或者当您知道URI将是什么并且它还不存在时创建一些新的东西(与POST相反,POST将创建一些东西并在必要时返回URL)。

值得一提的是,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,在发出实际请求之前需要进一步请求服务器的许可。]所谓的飞行前请求[..]