使用POST创建请求,响应代码为200或201,内容为

[英]Create request with POST, which response codes 200 or 201 and content


Suppose I write a REST service whose intent is to add a new data item to a system.

假设我编写了一个REST服务,其目的是将新数据项添加到系统中。

I plan to POST to

我计划发帖到

http://myhost/serviceX/someResources

Suppose that works, what response code should I use? And what content might I return.

假设有效,我应该使用什么样的响应代码?我可以回复什么内容。

I'm looking at the definitions of HTTP response codes and see these possibilities:

我正在查看HTTP响应代码的定义,并看到这些可能性:

200: Return an entity describing or containing the result of the action;

200:返回描述或包含动作结果的实体;

201: which means CREATED. Meaning *The request has been fulfilled and resulted in a new resource being created. The newly created resource can be referenced by the URI(s) returned in the entity of the response, with the most specific URI for the resource given by a Location header field. The response SHOULD include an entity containing a list of resource characteristics and location(s) from which the user or user agent can choose the one most appropriate. The entity format is specified by the media type given in the Content-Type header field. *

201:表示创建。含义*请求已完成并导致创建新资源。新创建的资源可以由响应实体中返回的URI引用,具有Location头字段给出的资源的最特定URI。响应应该包括一个实体,其中包含资源特征和位置的列表,用户或用户代理可以从中选择最合适的资源特征和位置。实体格式由Content-Type头字段中给出的媒体类型指定。 *

The latter sounds more in line with the Http spec, but I'm not at all clear what

后者听起来更符合Http规范,但我一点都不清楚

The response SHOULD include an entity containing a list of resource characteristics and location(s)

响应应该包括一个包含资源特征和位置列表的实体

means.

手段。

Recommendations? Interpretations?

建议?解释?

7 个解决方案

#1


9  

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.19

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.19

It's just a colon delimited key-value.

它只是一个以冒号分隔的键值。

ETag: "xyzzy"

ETag:“xyzzy”

It can be any type of text data - I generally include a JSON string with the identifier of the item created. The ease of testing alone makes including it worthwhile.

它可以是任何类型的文本数据 - 我通常包含一个JSON字符串,其中包含所创建项目的标识符。单独测试的简易性使得包括它值得。

ETag: "{ id: 1234, uri: 'http://domain.com/comments/1234', type: 'comment' }"

In this example, the identifier, the uri, and type of the created item are the "resource characteristics and location".

在此示例中,所创建项的标识符,uri和类型是“资源特征和位置”。

#2


77  

I think atompub REST API is a great example of a restful service. See the snippet below from the atompub spec:

我认为atompub REST API是一个很好的宁静服务示例。请参阅atompub规范中的以下代码段:

POST /edit/ HTTP/1.1
Host: example.org
User-Agent: Thingio/1.0
Authorization: Basic ZGFmZnk6c2VjZXJldA==
Content-Type: application/atom+xml;type=entry
Content-Length: nnn
Slug: First Post

<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom">
  <title>Atom-Powered Robots Run Amok</title>
  <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
  <updated>2003-12-13T18:30:02Z</updated>
  <author><name>John Doe</name></author>
  <content>Some text.</content>
</entry>

The server signals a successful creation with a status code of 201. The response includes a Location header indicating the Member Entry URI of the Atom Entry, and a representation of that Entry in the body of the response.

服务器用状态代码201表示成功创建。响应包括指示Atom Entry的成员条目URI的Location头,以及响应主体中该Entry的表示。

HTTP/1.1 201 Created
Date: Fri, 7 Oct 2005 17:17:11 GMT
Content-Length: nnn
Content-Type: application/atom+xml;type=entry;charset="utf-8"
Location: http://example.org/edit/first-post.atom
ETag: "c180de84f991g8"  

<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom">
  <title>Atom-Powered Robots Run Amok</title>
  <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
  <updated>2003-12-13T18:30:02Z</updated>
  <author><name>John Doe</name></author>
  <content>Some text.</content>
  <link rel="edit"
      href="http://example.org/edit/first-post.atom"/>
</entry>

The Entry created and returned by the Collection might not match the Entry POSTed by the client. A server MAY change the values of various elements in the Entry, such as the atom:id, atom:updated, and atom:author values, and MAY choose to remove or add other elements and attributes, or change element content and attribute values.

集合创建和返回的条目可能与客户端发布的条目不匹配。服务器可以更改Entry中各种元素的值,例如atom:id,atom:updated和atom:author values,并且可以选择删除或添加其他元素和属性,或者更改元素内容和属性值。

#3


36  

The idea is that the response body gives you a page that links you to the thing:

我们的想法是,响应主体会为您提供一个链接到该事物的页面:

201 Created

201创建

The 201 (Created) status code indicates that the request has been fulfilled and has resulted in one or more new resources being created. The primary resource created by the request is identified by either a Location header field in the response or, if no Location field is received, by the effective request URI.

201(已创建)状态代码表示已完成请求并已导致创建一个或多个新资源。由请求创建的主要资源由响应中的Location头字段标识,或者如果没有收到Location字段,则由有效请求URI标识。

This means that you would include a Location in the response header that gives the URL of where you can find the newly created thing:

这意味着您将在响应标头中包含一个位置,该位置提供您可以在哪里找到新创建的内容的URL:

HTTP/1.1 201 Created
Date: Sat, 02 Apr 2016 12:22:40 GMT
Location: http://stackoverflow.com/a/36373586/12597

Response body

They then go on to mention what you should include in the response body:

然后,他们继续提及您应该在响应正文中包含的内容:

The 201 response payload typically describes and links to the resource(s) created.

201响应有效负载通常描述并链接到创建的资源。

For the human using the browser, you give them something they can look at, and click, to get to their newly created resource:

对于使用浏览器的人,您可以给他们一些他们可以查看的内容,然后单击,以获取他们新创建的资源:

HTTP/1.1 201 Created
Date: Sat, 02 Apr 2016 12:22:40 GMT
Location: http://stackoverflow.com/a/36373586/12597
Content-Type: text/html

Your answer has been saved! 
Click <A href="/a/36373586/12597">here</A> to view it.

If the page will only be used by a robot, the it makes sense to have the response be computer readable:

如果页面仅由机器人使用,则使响应具有计算机可读性是有意义的:

HTTP/1.1 201 Created
Date: Sat, 02 Apr 2016 12:22:40 GMT
Location: http://stackoverflow.com/a/36373586/12597
Content-Type: application/xml

<createdResources>
   <questionID>1860645</questionID>
   <answerID>36373586</answerID>
   <primary>/a/36373586/12597</primary>
   <additional>
      <resource>http://stackoverflow.com/questions/1860645/create-request-with-post-which-response-codes-200-or-201-and-content/36373586#36373586</resource>
      <resource>http://stackoverflow.com/a/1962757/12597</resource>
   </additional>
</createdResource>

Or, if you prefer:

或者,如果您愿意:

HTTP/1.1 201 Created
Date: Sat, 02 Apr 2016 12:22:40 GMT
Location: http://stackoverflow.com/a/36373586/12597
Content-Type: application/json

{ 
   "questionID": 1860645, 
   "answerID": 36373586,
   "primary": "/a/36373586/12597",
   "additional": [
      "http://stackoverflow.com/questions/1860645/create-request-with-post-which-response-codes-200-or-201-and-content/36373586#36373586",
      "http://stackoverflow.com/a/36373586/12597"
   ]
}

The response is entirely up to you; it's arbitrarily what you'd like.

回应完全取决于你;这是你想要的任意。

Cache friendly

Finally there's the optimization that i can pre-cache the created resource (because i already have the content; i just uploaded it). The server can return a date or ETag which i can store with the content i just uploaded:

最后是优化,我可以预先缓存创建的资源(因为我已经有了内容;我只是上传了它)。服务器可以返回一个日期或ETag,我可以存储我刚刚上传的内容:

See Section 7.2 for a discussion of the meaning and purpose of validator header fields, such as ETag and Last-Modified, in a 201 response.

有关201响应中验证器头字段(如ETag和Last-Modified)的含义和用途的讨论,请参见第7.2节。

HTTP/1.1 201 Created
Date: Sat, 02 Apr 2016 12:22:40 GMT
Location: http://stackoverflow.com/a/23704283/12597
Content-Type: text/html
ETag: JF2CA53BOMQGU5LTOQQGC3RAMV4GC3LQNRSS4
Last-Modified: Sat, 02 Apr 2016 12:22:39 GMT 

Your answer has been saved! 
Click <A href="/a/36373586/12597">here</A> to view it.

And ETag s are purely arbitrary values. Having them be different when a resource changes (and caches need to be updated) is all that matters. The ETag is usually a hash (e.g. SHA2). But it can be a database rowversion, or an incrementing revision number. Anything that will change when the thing changes.

ETag是纯粹的任意价值观。当资源发生变化(并且需要更新缓存)时,让它们变得不同是最重要的。 ETag通常是散列(例如SHA2)。但它可以是数据库rowversion,也可以是递增的修订版号。当事物发生变化时会发生任何变化。

#4


21  

Check out HTTP: Method Definitions: POST.

查看HTTP:方法定义:POST。

The action performed by the POST method might not result in a resource that can be identified by a URI. In this case, either 200 (OK) or 204 (No Content) is the appropriate response status, depending on whether or not the response includes an entity that describes the result.

POST方法执行的操作可能不会生成可由URI标识的资源。在这种情况下,200(OK)或204(No Content)是适当的响应状态,具体取决于响应是否包括描述结果的实体。

If a resource has been created on the origin server, the response SHOULD be 201 (Created) and contain an entity which describes the status of the request and refers to the new resource, and a Location header (see section 14.30).

如果在源服务器上创建了资源,则响应应该是201(已创建)并包含描述请求状态的实体,并引用新资源和Location头(请参阅第14.30节)。

#5


11  

In a few words:

简单来说:

  • 200 when an object is created and returned
  • 200创建并返回对象时
  • 201 when an object is created but only its reference is returned (such as an ID or a link)
  • 201创建对象但仅​​返回其引用(例如ID或链接)时

#6


1  

The output is actually dependent on the content type being requested. However, at minimum you should put the resource that was created in Location. Just like the Post-Redirect-Get pattern.

输出实际上取决于所请求的内容类型。但是,至少应该放置在Location中创建的资源。就像Post-Redirect-Get模式一样。

In my case I leave it blank until requested otherwise. Since that is the behavior of JAX-RS when using Response.created().

在我的情况下,我将其留空,直到另有要求。因为这是使用Response.created()时JAX-RS的行为。

However, just note that browsers and frameworks like Angular do not follow 201's automatically. I have noted the behaviour in http://www.trajano.net/2013/05/201-created-with-angular-resource/

但是,请注意,像Angular这样的浏览器和框架不会自动跟踪201。我在http://www.trajano.net/2013/05/201-created-with-angular-resource/中注意到了这种行为

#7


-2  

Another answer I would have for this would be to take a pragmatic approach and keep your REST API contract simple. In my case I had refactored my REST API to make things more testable without resorting to JavaScript or XHR, just simple HTML forms and links.

我将要做的另一个答案是采取务实的方法并保持REST API合约的简单性。在我的情况下,我重构了我的REST API,以便在不使用JavaScript或XHR,只需简单的HTML表单和链接的情况下使事情更具可测性。

So to be more specific on your question above, I'd just use return code 200 and have the returned message contain a JSON message that your application can understand. Depending on your needs it may require the ID of the object that is newly created so the web application can get the data in another call.

因此,为了更具体地解决上面的问题,我只使用返回代码200并使返回的消息包含应用程序可以理解的JSON消息。根据您的需要,它可能需要新创建的对象的ID,以便Web应用程序可以在另一个调用中获取数据。

One note, in my refactored API contract, POST responses should not contain any cacheable data as POSTs are not really cachable, so limit it to IDs that can be requested and cached using a GET request.

请注意,在我重构的API合同中,POST响应不应包含任何可缓存的数据,因为POST实际上不可缓存,因此将其限制为可以使用GET请求请求和缓存的ID。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2009/12/07/7203f7872445d09c40f7622e12a273d5.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告