它们似乎都在向身体内部的服务器发送数据,那么它们有什么不同呢?
当前回答
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来创建或修改资源。
其他回答
实际上,除了他们的头衔之外,没有什么不同。GET和其他方法之间实际上有一个基本的区别。使用“GET”-Request方法,发送url地址行中的数据,首先用问号分隔,然后用&符号分隔。
但是使用“POST”-request方法,您不能通过url传递数据,而是必须将数据作为请求的所谓“主体”中的对象传递。在服务器端,您必须读取接收到的内容正文,以便获得发送的数据。 但另一方面,当你发送"GET"-Request时,就不可能在body中发送内容。
“GET”只用于获取数据而“POST”用于发布数据的说法是绝对错误的。没有人可以阻止你创建新的内容,删除现有的内容,编辑现有的内容或做任何在后端,基于数据,这是由“GET”请求或由“POST”请求发送。没有人能阻止你在后端编码,用一个"POST"-Request,客户端请求一些数据。
对于请求,无论使用哪种方法,您都调用URL并发送或不发送一些数据来指定,您希望将哪些信息传递给服务器以处理您的请求,然后客户端从服务器获得一个答案。数据可以包含你想要发送的任何东西,后端可以对数据做任何它想做的事情,响应可以包含任何你想要放在那里的信息。
只有这两个基本方法。GET和POST。但使它们不同的是它们的结构,而不是你在后端编写的代码。在后端,您可以使用接收到的数据编写任何您想要的代码。但是使用“POST”请求,你必须在正文中发送/检索数据,而不是在url-addressline中,而使用“GET”请求,你必须在url-addressline中发送/检索数据,而不是在正文中。这是所有。
所有其他的方法,比如"PUT", "DELETE"等等,它们都有和"POST"相同的结构。
如果你想隐藏一些内容,POST方法主要被使用,因为无论你在url-addressline中写什么,这都将被保存在缓存中,GET-Method与用数据写url-addressline是一样的。因此,如果你想发送敏感数据,不一定总是用户名和密码,但例如一些id或哈希,你不希望显示在url地址行,那么你应该使用POST方法。
此外,URL-Addressline的长度被限制为1024个符号,而“POST”-方法则不受限制。因此,如果数据量较大,则可能无法使用GET-Request发送数据,而需要使用POST-Request。这也是post请求的另一个优点。
但是当您没有复杂的文本要发送时,处理get请求要容易得多。 否则,这是POST方法的另一个优点,使用get方法,您需要对文本进行url编码,以便能够在文本甚至空格中发送一些符号。但是使用POST方法没有任何限制,您的内容不需要以任何方式更改或操作。
PUT是一种将内容“上载”到特定URI或覆盖该URI中已有内容的方法。
另一方面,POST是提交与给定URI相关的数据的一种方式。
参考HTTP RFC
根据HTTP方法定义操作
HTTP协议定义了许多为请求分配语义的方法。大多数RESTful web api使用的常见HTTP方法有:
GET在指定URI处检索资源的表示形式。响应消息的主体包含所请求资源的详细信息。
POST在指定的URI上创建一个新资源。请求消息的主体提供了新资源的详细信息。注意,POST还可以用于触发不实际创建资源的操作。
PUT在指定的URI上创建或替换资源。请求消息的主体指定要创建或更新的资源。
PATCH执行资源的部分更新。请求体指定要应用于资源的更改集。
DELETE删除指定URI上的资源。
特定请求的效果应取决于资源是集合还是单个项。下表通过电子商务示例总结了大多数RESTful实现采用的常见约定。并不是所有这些请求都可以实现——这取决于具体的场景。
Resource | POST | GET | PUT | DELETE |
---|---|---|---|---|
/customers | Create a new customer | Retrieve all customers | Bulk update of customers | Remove all customers |
/customers/1 | Error | Retrieve the details for customer 1 | Update the details of customer 1 if it exists | Remove customer 1 |
/customers/1/orders | Create a new order for customer 1 | Retrieve all orders for customer 1 | Bulk update of orders for customer 1 | Remove all orders for customer 1 |
POST、PUT和PATCH之间的区别可能令人困惑。
POST请求创建一个资源。服务器为新资源分配URI,并将该URI返回给客户端。在REST模型中,您经常对集合应用POST请求。新资源被添加到集合中。POST请求还可以用于将数据提交到现有资源进行处理,而不需要创建任何新资源。
A PUT request creates a resource or updates an existing resource. The client specifies the URI for the resource. The request body contains a complete representation of the resource. If a resource with this URI already exists, it is replaced. Otherwise, a new resource is created, if the server supports doing so. PUT requests are most frequently applied to resources that are individual items, such as a specific customer, rather than collections. A server might support updates but not creation via PUT. Whether to support creation via PUT depends on whether the client can meaningfully assign a URI to a resource before it exists. If not, then use POST to create resources and PUT or PATCH to update.
PATCH请求对现有资源执行部分更新。客户端为资源指定URI。请求体指定要应用于资源的一组更改。这可能比使用PUT更有效,因为客户端只发送更改,而不是资源的整个表示。从技术上讲,如果服务器支持的话,PATCH还可以创建一个新资源(通过指定一组对“null”资源的更新)。
PUT请求必须是等幂的。如果客户端多次提交相同的PUT请求,结果应该总是相同的(相同的资源将被修改为相同的值)。POST和PATCH请求不能保证是等幂的。
据我所知,PUT主要用于更新记录。
POST -创建文档或任何其他资源 PUT—更新创建的文档或任何其他资源。
但要清楚的是,PUT通常“替换”现有的记录,如果它在那里,如果它不在那里创建。
总结
使用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“主机”报头?
- 哪个HTTP状态代码表示“尚未准备好,稍后再试”?
- 如何阻止恶意代码欺骗“Origin”报头来利用CORS?
- 为什么说“HTTP是无状态协议”?
- 我需要HTTP GET请求的内容类型报头吗?
- 如何让Chrome允许混合内容?
- 正确的方式删除cookies服务器端
- REST DELETE真的是幂等的吗?
- 了解Chrome网络日志“停滞”状态
- jQuery发布JSON
- 用户代理字符串可以有多大?
- 什么是接受* HTTP报头q=0.5 ?
- HTTP状态码200(缓存)和状态码304之间有什么区别?
- HTTP POST返回错误:417“期望失败。”
- 什么是HTTP中的“406-不可接受的响应”?