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


当前回答

请参阅: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。PUT、DELETE等基本都是鸡毛蒜毛的问题。

请参阅: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做同样的事情。

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

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

参考HTTP RFC

只有语义。

HTTP PUT应该接受请求体,然后将其存储在由URI标识的资源中。

HTTP POST更为通用。它应该在服务器上发起一个操作。该操作可以是将请求体存储在由URI标识的资源中,也可以是不同的URI,也可以是不同的操作。

PUT类似于文件上传。对URI的put操作恰好影响该URI。对URI的POST可以产生任何效果。