我有一种情况,我需要我的 API 来调用触发服务端事件,客户端不需要任何信息(除了身份验证),并且服务器不需要返回任何信息。由于这不太适合标准的 CRUD/资源交互,我应该将此作为我做错了什么的指标,还是有 RESTful 设计模式来处理这些情况?
最佳答案
您的客户可以:
POST /trigger
服务器将响应
202 Accepted
.这样,您的请求仍然可以包含适当的身份验证 header ,并且如果您需要客户端提供实体,或者需要返回包含有关如何查询事件状态的信息的响应,API 可以在 future 扩展。
您在这里尝试做的事情没有任何“非 RESTful”; REST 原则不必与资源上的 CRUD 操作相关联。
spec for 202说:
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.
鉴于定义中的“应该”,您没有义务在响应中发送任何内容。
关于web-services - RESTful 触发服务器端事件的方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8492207/