我正在考虑创建一个 RESTful API 来监控远程仪器 - 出于讨论的目的,假设远程仪器是电压表。
API 的一个重要功能是随时读取仪器测量的电压。
我的问题:如果有的话,什么是合适的 HTTP 动词来读取一个值,该值可以从一个读数更改为下一个读数?
据我了解,GET 是错误的,因为服务器可能会假定该值是幂等的,因此会缓存返回值。
但其他常用选项——PUT 和 POST——似乎用于创建和更新值而不是返回值。
我可以想到几种方法:
- 在 header 中使用带有缓存控制的 GET:无缓存
- 使用(或滥用)POST 并让它返回一个值
- 不要为此类应用程序使用 RESTful 设计
什么是正确的方法?
最佳答案
My question: what, if any, is the appropriate HTTP verb for reading a value that can change from one reading to the next?
The GET method requests transfer of a current selected representation for the target resource.
缓存的语义在RFC 7234 中描述。 ;特别是,您可以使用 Cache-Control header向中间组件提供正确缓存响应所需的元数据。缓存控制指令列表包括 no-cache ,如果您始终希望中间组件在重用值之前检查来源。
这是 uniform interface 的要点- 因为资源统一地实现了一组一致的语义,客户和中介可以依赖这些语义来做出贡献,而不需要知道资源的具体细节、它的实现或它的表示。
The central feature that distinguishes the REST architectural style from other network-based styles is its emphasis on a uniform interface between components (Figure 5-6). By applying the software engineering principle of generality to the component interface, the overall system architecture is simplified and the visibility of interactions is improved. Implementations are decoupled from the services they provide, which encourages independent evolvability. The trade-off, though, is that a uniform interface degrades efficiency, since information is transferred in a standardized form rather than one which is specific to an application's needs. The REST interface is designed to be efficient for large-grain hypermedia data transfer, optimizing for the common case of the Web, but resulting in an interface that is not optimal for other forms of architectural interaction.
关于rest - 哪个 RESTful HTTP 动词用于读取时变状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49125292/