它们似乎都在向身体内部的服务器发送数据,那么它们有什么不同呢?
当前回答
请参阅: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用于更新给定URL上的现有数据,或者当您知道URI将是什么并且它还不存在时创建一些新的东西(与POST相反,POST将创建一些东西并在必要时返回URL)。
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。PUT将整个资源替换。示例:PUT /resources/:resourceId 旁注:如果您想更新资源的一部分,请使用PATCH。
当您希望在资源集合下添加子资源时,请使用POST。 示例:POST => /resources
一般来说:
通常,在实践中,总是使用PUT进行UPDATE操作。 CREATE操作总是使用POST。
例子:
GET /company/reports =>获取所有报告 GET /company/reports/{id} =>获取以"id"标识的报告信息 POST /company/reports => PUT /company/reports/{id} =>更新“id”标识的报告信息 PATCH /company/reports/{id} =>更新“id”标识的部分报告信息 DELETE /company/reports/{id} =>删除报表
总结
使用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.”
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.
推荐文章
- 如何使HTTP请求在PHP和不等待响应
- PATCH和PUT请求的主要区别是什么?
- 我可以把我所有的http://链接都改成//吗?
- URL为AJAX请求编码一个jQuery字符串
- 编译System.Net.HttpClient的查询字符串
- 摘要认证和基本认证的区别是什么?
- Axios -删除请求与请求体和头?
- 如何在http获取请求设置报头?
- 使用curl POST multipart/form-data的正确方法是什么?
- 如何使用Ruby on Rails进行HTTP请求?
- REST API最佳实践:查询字符串中的参数vs请求体中的参数
- 我如何调试一个HTTP POST在Chrome?
- 发送POST数据重定向与JavaScript/jQuery?
- Express.js中的res.send和res.json的区别
- 如何评估http响应代码从bash/shell脚本?