使用POST创建请求,其响应代码为200或201以及内容 [英] Create request with POST, which response codes 200 or 201 and content

查看:761
本文介绍了使用POST创建请求,其响应代码为200或201以及内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我打算过帐到

http://myhost/serviceX/someResources

假设可行,我应该使用什么响应代码?我可能会返回什么内容。

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

我正在查看 HTTP响应代码的定义,并查看以下可能性:

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

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

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

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. *

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

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


响应应包含实体
,其中包含资源
的特征和位置列表

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

是什么意思。

建议?

推荐答案

想法是,响应正文为您提供一个将您链接到事物的页面:

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


201 Created

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

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.

这意味着您将在响应 header 中包含 Location ,该URL给出了在其中可以找到新创建的事物

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:

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



响应正文



然后他们继续提及您应该在响应 body 中包括什么:


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

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

对于使用浏览器的人类,您给他们看的东西,然后单击以进入他们新创建的资源:

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>

或者,如果您愿意:

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"
   ]
}

响应完全取决于您;

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

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:


请参见第7.2 节讨论了验证标头字段(例如ETag和Last-Modified)的含义和目的。 201响应。

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.



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.

ETag s纯粹是任意值。当资源更改(和缓存需要更新)时,让它们有所不同就很重要。 ETag通常是一个散列(例如SHA2)。但是它可以是数据库 rowversion 或递增的修订号。当事物发生变化时,任何会变化的事物。

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.

这篇关于使用POST创建请求,其响应代码为200或201以及内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆