简体   繁体   English

RESTful API设计通过相同的HTTP方法区分对相同URI的操作

[英]RESTful API design differentiate operations on identical URI with same HTTP Method

I would like to design a RESTful API in order to control a Bluetooth Dongle over a web interface. 我想设计一个RESTful API,以便通过Web界面控制Bluetooth Dongle。

What happens if I have the following conflict 如果我有以下冲突怎么办

POST /IDofDongle/

Let's say I want this specific USB dongle to either start a discovery/inquiry process (append information about surrounding Bluetooth devices to the resource) or connect to any Bluetooth device (create a new subordinate resource by appending the MAC Address of the Bluetooth device to the URI above)? 假设我想让这个特定的USB加密狗启动发现/查询过程(将有关周围蓝牙设备的信息添加到资源中)或连接到任何蓝牙设备(通过将蓝牙设备的MAC地址附加到蓝牙设备来创建新的从属资源)上面的URI)?

Differentiating using a QueryString seems very RPC like to me. 在我看来,使用QueryString进行区分似乎非常类似于RPC。 Or do I need to define other resources? 还是我需要定义其他资源?

You've run into the REST fallacy. 您遇到了REST错误。 Not everything is a resource, so you shouldn't try to treat everything as such. 并非所有内容都是资源,因此您不应尝试将所有内容都视为这样。 By POSTint to that endpoint, you're saying you want to create an USB dongle. 通过POSTint到该端点,就意味着您要创建USB加密狗。 As you aren't accessing a 3D printer, this won't work. 由于您不访问3D打印机,因此无法使用。 You are building an RPC service. 正在构建RPC服务。

So break with REST and introduce your own verbs: 因此,请打破REST并引入自己的动词:

POST /IDofDongle/Discover
POST /IDofDongle/Connect

Of course you could treat "discoveries" and "connections" as a resource: 当然,您可以将“发现”和“连接”视为资源:

POST /IDofDongle/Discoveries
POST /IDofDongle/Connections

"Creating" those entities. “创建”那些实体。 You can then return a Discovery or Connection entity that relates to the specified dongle, holding the relevant properties for such entities (DiscoveredDevices, ConnectionID, ...). 然后,您可以返回与指定的加密狗相关的发现或连接实体,其中包含此类实体的相关属性(DiscoveredDevices,ConnectionID等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM