它们似乎都在向身体内部的服务器发送数据,那么它们有什么不同呢?
当前回答
总结
使用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.”
其他回答
请参阅: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应该接受请求体,然后将其存储在由URI标识的资源中。
HTTP POST更为通用。它应该在服务器上发起一个操作。该操作可以是将请求体存储在由URI标识的资源中,也可以是不同的URI,也可以是不同的操作。
PUT类似于文件上传。对URI的put操作恰好影响该URI。对URI的POST可以产生任何效果。
简单来说,你可以说:
1.HTTP Get:用于获取一个或多个条目
2.HTTP Post:用于创建条目
3.HTTP Put:用于更新条目
4.HTTP补丁:用于部分更新项目
5.HTTP删除:用于删除项目
PUT是一种将内容“上载”到特定URI或覆盖该URI中已有内容的方法。
另一方面,POST是提交与给定URI相关的数据的一种方式。
参考HTTP RFC
据我所知,PUT主要用于更新记录。
POST -创建文档或任何其他资源 PUT—更新创建的文档或任何其他资源。
但要清楚的是,PUT通常“替换”现有的记录,如果它在那里,如果它不在那里创建。
推荐文章
- HTTP 1.1和HTTP 2.0的区别是什么?
- 什么是“升级-不安全-请求”HTTP报头?
- 我如何捕捉Ajax查询后错误?
- HTTP 301和308状态码有什么区别?
- 什么HTTP状态码应该用于错误的输入
- application/json和application/x-www-form-urlencoded有什么区别?
- 编排microservices
- 如何使HTTP请求在PHP和不等待响应
- PATCH和PUT请求的主要区别是什么?
- 我可以把我所有的http://链接都改成//吗?
- URL为AJAX请求编码一个jQuery字符串
- 编译System.Net.HttpClient的查询字符串
- 摘要认证和基本认证的区别是什么?
- Axios -删除请求与请求体和头?
- 如何在http获取请求设置报头?