它们似乎都在向身体内部的服务器发送数据,那么它们有什么不同呢?
当前回答
POST和PUT之间的区别在于PUT是幂等的,这意味着多次调用相同的PUT请求总是会产生相同的结果(没有副作用),而另一方面,重复调用POST请求可能会产生多次创建相同资源的(额外的)副作用。
GET:使用GET的请求只检索数据,也就是说它请求指定资源的表示
POST:向服务器发送数据以创建资源。请求体的类型由Content-Type报头表示。它通常会导致服务器的状态变化或副作用
PUT:创建一个新资源或用请求有效负载替换目标资源的表示
PATCH:用于对资源应用部分修改
DELETE:删除指定资源
TRACE:它沿着通往目标资源的路径执行消息循环测试,提供了一种有用的调试机制
OPTIONS:用于描述目标资源的通信选项,客户端可以为OPTIONS方法指定一个URL,或者一个星号(*)来引用整个服务器。
HEAD:它请求与GET请求相同的响应,但没有响应体
CONNECT:它建立到目标资源标识的服务器的隧道,可用于访问使用SSL (HTTPS)的网站
其他回答
POST被认为是某种工厂类型的方法。您将数据包含在其中以创建您想要的内容,而另一端的任何内容都知道如何使用它。PUT用于更新给定URL上的现有数据,或者当您知道URI将是什么并且它还不存在时创建一些新的东西(与POST相反,POST将创建一些东西并在必要时返回URL)。
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主要用于发布数据和其他更新数据。但是你可以只对post request做同样的事情。
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.
什么时候使用一种或另一种应该是相当简单的,但复杂的措辞对我们许多人来说是困惑的来源。
何时使用:
当您希望修改已经是资源集合一部分的单个资源时,请使用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} =>删除报表
推荐文章
- 什么是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-不可接受的响应”?