REST后可以在POST后返回内容吗? [英] Is it ok by REST to return content after POST?

查看:100
本文介绍了REST后可以在POST后返回内容吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RESTlet,我已经创建了一个资源。我通过覆盖acceptRepresentation方法来处理POST。

I am using RESTlet and I have created a resource. I handle POST by overriding acceptRepresentation method.

客户端应该向我发送一些数据,然后我将它存储到DB,将响应设置为201(SUCCESS_CREATED)并且我需要返回一些数据到客户端,但是acceptRepresentation的返回类型是无效的。

Client should send me some data, then I store it to DB, set response to 201 (SUCCESS_CREATED) and I need to return some data to client, but return type of acceptRepresentation is void.

在我的情况下,我需要返回一些标识符,以便客户端可以访问该资源。

In my case I need to return some identificator so that client can access that resource.

例如,如果我有一个带有URL /资源的资源而客户端发送POST请求,我在DB中添加新行,其地址应为/ resource / {id}。我需要发送{id}。

For example, if I had a resource with URL /resource and client sends POST request I add new row in DB and its address should be /resource/{id}. I need to send {id}.

我做错了什么? REST原则是否允许在POST后返回一些内容?如果是的话,我怎么能这样做,如果没有办法处理这种情况呢?

Am I doing something wrong? Does REST principles allow to return something after POST? If yes, how can I do it, and if no what is the way to handle this situation?

推荐答案

REST只是说你应该遵循统一的界面。换句话说,它表示你应该根据 HTTP规范。以下是相关规范的引用,

REST just says that you should conform to the uniform interface. In other words, it says you should do what POST is supposed to do as per the HTTP spec. Here is the quote from that spec that is relevant,


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

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

从这里可以看出,您有两个地方可以向客户指示新创建的资源驻留。 Location标头应该有一个指向新资源的URL,你也可以返回一个带有详细信息的实体。

As you can see from this, you have two places where you can indicate to the client where the newly created resource resides. The Location header should have an URL that points to the new resource and you can return an entity with the details also.

我不知道重写acceptRepresentation有什么区别()和覆盖post(),但示例显示了如何从POST返回响应。

I'm not sure what the difference between overriding acceptRepresentation() and overriding post() but this example shows how to return a response from a POST.

这篇关于REST后可以在POST后返回内容吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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