它们似乎都在向身体内部的服务器发送数据,那么它们有什么不同呢?
当前回答
什么时候使用一种或另一种应该是相当简单的,但复杂的措辞对我们许多人来说是困惑的来源。
何时使用:
当您希望修改已经是资源集合一部分的单个资源时,请使用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是一种将内容“上载”到特定URI或覆盖该URI中已有内容的方法。
另一方面,POST是提交与给定URI相关的数据的一种方式。
参考HTTP RFC
据我所知,PUT主要用于更新记录。
POST -创建文档或任何其他资源 PUT—更新创建的文档或任何其他资源。
但要清楚的是,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} =>删除报表
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.
其他人已经发布了很好的答案,我只是想补充一点,在大多数语言、框架和用例中,你会更多地使用POST,而不是PUT。PUT、DELETE等基本都是鸡毛蒜毛的问题。
推荐文章
- 什么是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-不可接受的响应”?