它们似乎都在向身体内部的服务器发送数据,那么它们有什么不同呢?
当前回答
PUT是一种将内容“上载”到特定URI或覆盖该URI中已有内容的方法。
另一方面,POST是提交与给定URI相关的数据的一种方式。
参考HTTP RFC
其他回答
请参阅: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来创建或修改资源。
PUT是一种将内容“上载”到特定URI或覆盖该URI中已有内容的方法。
另一方面,POST是提交与给定URI相关的数据的一种方式。
参考HTTP RFC
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是幂等操作:如果对同一个请求执行多次,它将没有副作用。
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.
推荐文章
- 如何阻止恶意代码欺骗“Origin”报头来利用CORS?
- 为什么说“HTTP是无状态协议”?
- 我需要HTTP GET请求的内容类型报头吗?
- 如何让Chrome允许混合内容?
- 正确的方式删除cookies服务器端
- REST DELETE真的是幂等的吗?
- 了解Chrome网络日志“停滞”状态
- jQuery发布JSON
- 用户代理字符串可以有多大?
- 什么是接受* HTTP报头q=0.5 ?
- HTTP状态码200(缓存)和状态码304之间有什么区别?
- HTTP POST返回错误:417“期望失败。”
- 什么是HTTP中的“406-不可接受的响应”?
- 最好的轻量级web服务器(只有静态内容)的Windows
- HTTP POST在Java中使用JSON