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

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

问题描述

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

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

客户端应该向我发送一些数据,然后我将其存储到数据库中,将响应设置为 201(SUCCESS_CREATED)并且我需要向客户端返回一些数据,但是返回类型 acceptRepresentation 是 <代码>无效.

The 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 the 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 为 /resource 的资源,并且客户端发送 POST 请求,我会在数据库中添加一个新行,其地址应为 /resource/{id}.我需要发送 {id}.

For example, if I had a resource with URL /resource and the client sends POST request I add a 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(已创建)并包含一个实体它描述了状态请求并引用新的资源和位置标头(见第 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() 之间有什么区别,但是 this 示例展示了如何从 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天全站免登陆