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


当前回答

其他人已经发布了很好的答案,我只是想补充一点,在大多数语言、框架和用例中,你会更多地使用POST,而不是PUT。PUT、DELETE等基本都是鸡毛蒜毛的问题。

其他回答

简单来说,你可以说:

1.HTTP Get:用于获取一个或多个条目

2.HTTP Post:用于创建条目

3.HTTP Put:用于更新条目

4.HTTP补丁:用于部分更新项目

5.HTTP删除:用于删除项目

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

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来创建或修改资源。

其他人已经发布了很好的答案,我只是想补充一点,在大多数语言、框架和用例中,你会更多地使用POST,而不是PUT。PUT、DELETE等基本都是鸡毛蒜毛的问题。

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.