RESTful方式触发服务器端事件
我遇到一种情况,我需要 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的客户端可以:
服务器将响应
202 Accepted
。这样,您的请求仍然可以包含适当的身份验证标头,并且如果您需要客户端提供实体,或者需要返回包含有关如何查询事件状态的信息的响应,则将来可以扩展 API。
您在这里尝试做的事情没有什么“非 RESTful”; REST 原则不必与资源上的 CRUD 操作相关联。
202 规范说:
鉴于定义中的“应该”,您没有义务在响应中发送任何内容。
Your client can just:
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:
You aren't obliged to send anything in the response, given the "SHOULD" in the definition.
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?