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

总体:

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和REST杰出人士开发,并由REST的发明者和HTTP的(共同)发明者Roy Fielding提供了一些输入

事实上,您甚至可以直接使用AtomPub。虽然它来自博客社区,但它绝不局限于博客:它是一个通用协议,用于通过HTTP与任意资源的任意(嵌套)集合进行RESTfully交互。如果您可以将应用程序表示为一个嵌套的资源集合,那么您可以只使用AtomPub,而不必担心是使用PUT还是POST、返回什么HTTP状态代码以及所有这些细节

这就是AtomPub对资源创建的看法(第9.2节):

要向集合添加成员,客户端将POST请求发送到集合的URI

  • 发布到URL在服务器定义的URL上创建子资源
  • 放入URL在客户端定义的URL处创建/替换整个资源
  • 修补到URL更新该客户端定义的URL上的部分资源
PUT和POST的相关规范如下:

POST创建一个子资源,因此POST to
/items
将创建一个位于
/items
资源下的资源。 例如,
/items/1
。两次发送相同的post数据包将创建两个资源

PUT用于在客户端已知的URL上创建或替换资源

因此:PUT只是创建的候选项,其中客户端在创建资源之前已经知道url。例如,
/blogs/nigel/entry/when\u to\u use\u post\u vs\u put
作为标题用作资源键

PUT替换已知url上已存在的资源,因此两次发送同一请求无效。换句话说,对PUT的调用是幂等的

PUT /resources/<newResourceId> HTTP/1.1 
PUT /resources/<existingResourceId> HTTP/1.1
RFC的内容如下:

POST /users/john HTTP/1.1
POST和PUT请求之间的根本区别体现在请求URI的不同含义上。POST请求中的URI标识将处理封闭实体的资源。该资源可能是一个数据接受进程、到其他协议的网关,或者是一个接受注释的单独实体。相反
URI: website.com/users/john
website.com  - whole site
users        - collection of users
john         - item of the collection, or a resource

URI:website.com/users/john/posts/23
website.com  - whole site
users        - collection of users
john         - item of the collection, or a resource
posts        - collection of posts from john
23           - post from john with identifier 23, also a resource
POST /users HTTP/1.1
POST /users/john HTTP/1.1
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.