RESTful方式触发服务器端事件

发布于 2024-12-21 06:10:45 字数 135 浏览 3 评论 0原文

我遇到一种情况,我需要 API 来调用来触发服务端事件,客户端不需要任何信息(除了身份验证),并且服务器不需要返回任何信息。由于这不太适合标准的 CRUD/资源交互,我是否应该将此视为我做错了什么的指标,或者是否有 RESTful 设计模式来处理这些情况?

I have a situation where I need my API to have a call for triggering a service-side event, no information (besides authentication) is needed from the client, and nothing needs to be returned by the server. Since this doesn't fit well into the standard CRUD/Resource interaction, should I take this as an indicator that I'm doing something wrong, or is there a RESTful design pattern to deal with these conditions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我太投入 2024-12-28 06:10:45

您的客户端可以:

POST /trigger

服务器将响应 202 Accepted

这样,您的请求仍然可以包含适当的身份验证标头,并且如果您需要客户端提供实体,或者需要返回包含有关如何查询事件状态的信息的响应,则将来可以扩展 API。

您在这里尝试做的事情没有什么“非 RESTful”; REST 原则不必与资源上的 CRUD 操作相关联。


202 规范说:

随此响应返回的实体应该包含以下指示
请求的当前状态以及指向状态监视器的指针
或用户预计请求何时出现的一些估计
实现了。

鉴于定义中的“应该”,您没有义务在响应中发送任何内容。

Your client can just:

POST /trigger

To which the server would respond with a 202 Accepted.

That way your request can still contain the appropriate authentication headers, and the API can be extended in the future if you need the client to supply an entity, or need to return a response with information about how to query the event status.

There's nothing "non-RESTful" about what you're trying to do here; REST principles don't have to correlate to CRUD operations on resources.


The spec for 202 says:

The entity returned with this response SHOULD include an indication of
the request's current status and either a pointer to a status monitor
or some estimate of when the user can expect the request to be
fulfilled.

You aren't obliged to send anything in the response, given the "SHOULD" in the definition.

星星的軌跡 2024-12-28 06:10:45

REST 定义了客户端和服务器之间通信的性质。在这种情况下,我认为问题是没有信息可以传输。

客户有什么理由需要启动此操作吗?我想说你的服务器端事件应该完全独立于服务器内。也许通过 cron 调用定期启动它?

REST defines the nature of the communication between the client and server. In this case, I think the issues is there is no information to transfer.

Is there any reason the client needs to initiate this at all? I'd say your server-side event should be entirely self-contained within the server. Perhaps kick it off periodically with a cron call?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文