JSON API: 没有资源的成功信息

JSON API: Succes Message without resource

我们正在使用 JSON-API 标准来开发我们的 API 并且我们遇到了一个问题,该问题似乎没有遵循标准的明显解决方案。

用例如下:

There is an API endpoint that allows you to subscribe to mailinglists. One possible flow is that the user is added with as PENDING which means that the user will receive and opt-in email for confirmation.

If this is the case we want to give back a message to the frontend that can be displayed to the user, urging him to click on the link.

从我的角度来看,这并不是真正的错误状态,更多的是后续元信息。所以这意味着将它放在错误消息中在概念上是不合逻辑的。此外,如果我们确实将其放入错误消息中,前端必须以某种方式将其与 'the real errors' 区分开来(状态码的分辨率如此之低,以至于冲突似乎不可避免)。

但是我们没有 return 资源,因此我们无法将其作为元信息添加到资源中。所以现在我不知道把这些信息放在哪里。

一个可能的解决方案是定义某种 'Response' 资源并将其放入其中,但这看起来就像一堆蠕虫。

有什么想法吗?输入将非常有价值

如果调用结果是用户被添加到邮件列表,return一个200 OK。如果通话结果是用户必须通过电子邮件选择加入,return a 202 Accepted。 Return 一个包含相关信息的 202 响应对象。

来自规范:

The 202 (Accepted) status code indicates that the request has been accepted for processing, but the processing has not been completed. The request might or might not eventually be acted upon, as it might be disallowed when processing actually takes place. There is no facility in HTTP for re-sending a status code from an asynchronous operation.

The 202 response is intentionally noncommittal. Its purpose is to allow a server to accept a request for some other process (perhaps a batch-oriented process that is only run once per day) without requiring that the user agent's connection to the server persist until the process is completed. The representation sent with this response ought to describe the request's current status and point to (or embed) a status monitor that can provide the user with an estimate of when the request will be fulfilled.