REST中的PUT与POST [英] PUT vs. POST in REST
问题描述
根据HTTP / 1.1规范:
According to the HTTP/1.1 Spec:
POST
方法用于请求原始服务器接受请求中包含的实体作为Request-URI
标识的资源的新下级请求行
The
POST
method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by theRequest-URI
in theRequest-Line
换句话说, POST
用于创建。
PUT
方法请求将所包含的实体存储在提供的Request-URI
下。如果Request-URI
引用已存在的资源,则封闭的实体应该被视为驻留在源服务器上的实体的修改版本。如果Request-URI
未指向现有资源,并且该URI能够被请求用户代理定义为新资源,则源服务器可以创建具有该URI的资源。
The
PUT
method requests that the enclosed entity be stored under the suppliedRequest-URI
. If theRequest-URI
refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server. If theRequest-URI
does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI."
即 PUT
用于创建或更新。
那么,应该使用哪一个创建资源?或者需要同时支持两者?
So, which one should be used to create a resource? Or one needs to support both?
推荐答案
总体来说:
PUT和POST都可以使用用于创建。
Both PUT and POST can be used for creating.
您必须询问您要执行的操作是什么?以区分您应该使用的内容。让我们假设您正在设计一个API用于询问问题。如果你想使用POST,那么你会想到一个问题列表。如果你想使用PUT那么你就可以做到特定的问题。
You have to ask "what are you performing the action to?" to distinguish what you should be using. Let's assume you're designing an API for asking questions. If you want to use POST then you would do that to a list of questions. If you want to use PUT then you would do that to a particular question.
两者都可以使用,所以我应该在RESTful设计中使用哪一个:
您不需要同时支持PUT和POST。
You do not need to support both PUT and POST.
使用哪种方法取决于你。但请记住使用正确的,具体取决于您在请求中引用的对象。
Which is used is left up to you. But just remember to use the right one depending on what object you are referencing in the request.
一些注意事项:
- 您是否明确指定了您创建的URL对象,还是让服务器决定?如果您为它们命名,那么使用PUT。如果您让服务器决定然后使用POST。
- PUT是幂等的,所以如果您将对象PUT两次,它就没有效果。这是一个很好的属性,所以我会尽可能使用PUT。
- 您可以使用相同的对象URL更新或创建具有PUT的资源
- 使用POST,您可以同时进行2个请求对URL的修改,它们可以更新对象的不同部分。
- Do you name your URL objects you create explicitly, or let the server decide? If you name them then use PUT. If you let the server decide then use POST.
- PUT is idempotent, so if you PUT an object twice, it has no effect. This is a nice property, so I would use PUT when possible.
- You can update or create a resource with PUT with the same object URL
- With POST you can have 2 requests coming in at the same time making modifications to a URL, and they may update different parts of the object.
例如:
POST:
用于修改和更新资源
POST /questions/<existing_question> HTTP/1.1
Host: www.example.com/
请注意,以下是错误:
POST /questions/<new_question> HTTP/1.1
Host: www.example.com/
如果网址还没有创建后,
在指定名称时不应使用POST创建
。这应该是
会导致'找不到资源'错误
,因为< new_question>
还不存在
。您应首先在服务器上输入< new_question>
资源。
If the URL is not yet created, you
should not be using POST to create it
while specifying the name. This should
result in a 'resource not found' error
because <new_question>
does not exist
yet. You should PUT the <new_question>
resource on the server first.
您可以这样做像
这样用POST来创建资源:
You could though do something like this to create a resources using POST:
POST /questions HTTP/1.1
Host: www.example.com/
请注意,在这种情况下,未指定资源
名称,新对象
URL路径将返回给您。
Note that in this case the resource name is not specified, the new objects URL path would be returned to you.
PUT:
用于创建资源,或
覆盖它。当您指定
资源新网址时。
Used to create a resource, or overwrite it. While you specify the resources new URL.
对于新资源:
PUT /questions/<new_question> HTTP/1.1
Host: www.example.com/
要覆盖现有资源:
PUT /questions/<existing_question> HTTP/1.1
Host: www.example.com/
这篇关于REST中的PUT与POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!