HTTP中POST和PUT的区别是什么?

HTTP中POST和PUT的区别是什么?,http,rest,post,put,Http,Rest,Post,Put,根据,POST用于创建资源: PUT /questions/<new_question> HTTP/1.1 Host: www.example.com/ PUT /questions/<existing_question> HTTP/1.1 Host: www.example.com/ POST方法用于请求源服务器接受包含在请求中的实体,作为由请求行中的请求URI标识的资源的新从属 根据,PUT用于创建或替换资源: PUT /questions/<new_que

根据,
POST
用于创建资源:

PUT /questions/<new_question> HTTP/1.1
Host: www.example.com/
PUT /questions/<existing_question> HTTP/1.1
Host: www.example.com/
POST方法用于请求源服务器接受包含在请求中的实体,作为由请求行中的请求URI标识的资源的新从属

根据,
PUT
用于创建或替换资源:

PUT /questions/<new_question> HTTP/1.1
Host: www.example.com/
PUT /questions/<existing_question> HTTP/1.1
Host: www.example.com/
PUT方法请求将封闭的实体存储在提供的请求URI下。如果请求URI引用的是一个已经存在的资源,则应将包含的实体视为驻留在源服务器上的实体的修改版本。如果请求URI不指向现有资源,并且该URI能够由请求用户代理定义为新资源,则源服务器可以使用该URI创建资源


那么应该使用哪个HTTP方法来创建资源呢?或者两者都应该支持?

使用POST创建,并将其置于更新状态。无论如何,RubyonRails就是这样做的

PUT    /items/1      #=> update
POST   /items        #=> create

使用POST创建,并将其放入更新。无论如何,RubyonRails就是这样做的

PUT    /items/1      #=> update
POST   /items        #=> create

总体:

PUT和POST都可以用于创建

你必须问,“你在做什么?”来区分你应该使用什么。假设您正在设计一个用于提问的API。如果您想使用POST,那么您可以对一系列问题执行此操作。如果你想使用PUT,那么你会对一个特定的问题这样做

太好了,两者都可以使用,那么在我的RESTful设计中应该使用哪一种:

您不需要同时支持PUT和POST

你用什么取决于你自己。但是请记住,根据您在请求中引用的对象,使用正确的方法

一些考虑:

  • 您是显式命名您创建的URL对象,还是由服务器决定?如果你给它们命名,那么就用PUT。如果由服务器决定,则使用POST
  • PUT的定义是假定幂等性,所以如果将一个对象放置两次,它就不会有额外的效果。这是一个很好的属性,所以如果可能,我会使用PUT。只需确保PUT幂等性在服务器中得到了正确的实现
  • 您可以使用具有相同对象URL的PUT更新或创建资源
  • 使用POST,您可以同时收到两个请求,对URL进行修改,它们可能会更新对象的不同部分
一个例子:

我写了以下内容作为本书的一部分:

帖子:

用于修改和更新资源

POST /questions/<existing_question> HTTP/1.1
Host: www.example.com/
请注意,在本例中,资源 如果未指定名称,则将创建新对象 URL路径将返回给您

PUT:

用于创建资源,或 覆盖它。当您指定 参考资料新网址

对于新资源:

PUT /questions/<new_question> HTTP/1.1
Host: www.example.com/
PUT /questions/<existing_question> HTTP/1.1
Host: www.example.com/
PUT/questions/HTTP/1.1
主持人:www.example.com/
要覆盖现有资源,请执行以下操作:

PUT /questions/<new_question> HTTP/1.1
Host: www.example.com/
PUT /questions/<existing_question> HTTP/1.1
Host: www.example.com/
PUT/questions/HTTP/1.1
主持人:www.example.com/
此外,更简洁地说,声明(添加了强调)

4.3.4。放置

PUT方法请求目标资源的状态为
已创建
已用表示定义的状态替换
包含在请求消息有效负载中


总体:

PUT和POST都可以用于创建

你必须问,“你在做什么?”来区分你应该使用什么。假设您正在设计一个用于提问的API。如果您想使用POST,那么您可以对一系列问题执行此操作。如果你想使用PUT,那么你会对一个特定的问题这样做

太好了,两者都可以使用,那么在我的RESTful设计中应该使用哪一种:

您不需要同时支持PUT和POST

你用什么取决于你自己。但是请记住,根据您在请求中引用的对象,使用正确的方法

一些考虑:

  • 您是显式命名您创建的URL对象,还是由服务器决定?如果你给它们命名,那么就用PUT。如果由服务器决定,则使用POST
  • PUT的定义是假定幂等性,所以如果将一个对象放置两次,它就不会有额外的效果。这是一个很好的属性,所以如果可能,我会使用PUT。只需确保PUT幂等性在服务器中得到了正确的实现
  • 您可以使用具有相同对象URL的PUT更新或创建资源
  • 使用POST,您可以同时收到两个请求,对URL进行修改,它们可能会更新对象的不同部分
一个例子:

我写了以下内容作为本书的一部分:

帖子:

用于修改和更新资源

POST /questions/<existing_question> HTTP/1.1
Host: www.example.com/
请注意,在本例中,资源 如果未指定名称,则将创建新对象 URL路径将返回给您

PUT:

用于创建资源,或 覆盖它。当您指定 参考资料新网址

对于新资源:

PUT /questions/<new_question> HTTP/1.1
Host: www.example.com/
PUT /questions/<existing_question> HTTP/1.1
Host: www.example.com/
PUT/questions/HTTP/1.1
主持人:www.example.com/
要覆盖现有资源,请执行以下操作:

PUT /questions/<new_question> HTTP/1.1
Host: www.example.com/
PUT /questions/<existing_question> HTTP/1.1
Host: www.example.com/
PUT/questions/HTTP/1.1
主持人:www.example.com/
此外,更简洁地说,声明(添加了强调)

4.3.4。放置

PUT方法请求目标资源的状态为
已创建
已用表示定义的状态替换
包含在请求消息有效负载中

REST是一个非常高级的概念。事实上,它甚至根本没有提到HTTP

如果您对如何在HTTP中实现REST有任何疑问,可以随时查看规范。AtomPub是使用HTTP编写RESTful Web服务的标准,由许多HTTP和RES开发
PUT /users/john HTTP/1.1
Create => HTTP PUT
Retrieve => HTTP GET
Update => HTTP POST
Delete => HTTP DELETE
The client includes all aspect of the resource including the unique identifier to uniquely identify the resource. Example: creating a new employee.
The client provides all the information for a resource to be able to modify that resource.This implies that the server side does not update any aspect of the resource (such as an update date).
The server will provide some information concerning the newly created resource. For example, take a logging system. A new entry in the log will most likely have a numbering scheme which is determined on the server side. Upon creating a new log entry, the new sequence number will be determined by the server and not by the client.
On a modification of a resource, the server will provide such information as a resource state or an update date. Again in this case not all information was provided by the client and the resource will be changing from one modification request to the next. Hence a non idempotent operation.
1. PUT = UPDATE (/api/products/id)
2. MCSD Exams 2014 -  UPDATE = PUT, there are **NO** multiple answers for that question period.