REST 服务 - 公开非数据“操作”

发布于 2024-08-25 08:29:31 字数 199 浏览 3 评论 0原文

我了解如何使用 REST 进行一般实体交互 - 使用 url 名称映射到实体,使用 HTTP 动词映射到这些实体上的操作。但是,普遍接受的看待“动作”更像 RPC 的方式是什么?

例如,假设我想发送一个命令来重置设备?这里没有真正的“实体”,或者我是否执行类似 POST 到 http://mydevice/device/reset 的操作?

I understand how to use REST for doing general entity interactions - using urls names to map to entities and the HTTP verbs to map to actions on those entities. But what is the generally accepted way of looking at "actions" more like RPC?

For example, let's say I want to send a command for the device to reset? There's no real "entity" here or do I do something like POST to http://mydevice/device/reset?

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

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

发布评论

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

评论(3

[浮城] 2024-09-01 08:29:31

/device/reset/system/reset 都可以。

REST“设计模式”确实鼓励您不要使用任何动词。您可以这样做:

POST http://mydevice/system/state    
<stateType>RESET</stateType>

相关信息:

/device/reset or /system/reset are ok.

The REST "design pattern" does encourage you to NOT use any verbs.. You could do:

POST http://mydevice/system/state    
<stateType>RESET</stateType>

Related information:

仅此而已 2024-09-01 08:29:31

我不认为使用 POST 就是这种情况。 “RESET 操作”是一个幂等操作(如果您调用它 n 次,您将始终得到相同的结果),因此恕我直言,您应该使用 PUT 调用而不是 POST (因为 POST 不是幂等的)。

另外,当您放置资源时,您可以使用

PUT http://system
<device>
  <status>RESET</status>
</device>

or

 PUT http://system/status/reset

但我认为第一个是“更安静”,因为您正在放置资源,而第二个您只需使用 URL。

I don't think that's the case to use POST. The "RESET action" is a idempotent action (if you call it n times you will always get the same result), so IMHO you should use a PUT call instead POST (as POST is not idempotent).

Also, as you are Putting a resource, you can use

PUT http://system
<device>
  <status>RESET</status>
</device>

or

 PUT http://system/status/reset

But I think the first one is "more restful", since you are putting a resource, while the second one you just use the URL.

猛虎独行 2024-09-01 08:29:31

我通常将实体命名为“系统”或类似的名称。所以你执行“/system/reset”。您已经选择了设备,因此它也可以工作。

但是,是的,我通常认为这些类型的操作是更新,这将使用 POST 方法。所以我认为你 POST 到 /device/reset 是正确的

I usually name the entity "system" or something like that. So you do "/system/reset". You've chosen device so that works too.

But yea, I usually consider these types of actions to be updates, which would use the POST method. So I think you are right to POST to /device/reset

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