rest - 哪个 RESTful HTTP 动词用于读取时变状态?

标签 rest http http-headers

我正在考虑创建一个 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?

GET

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/

相关文章:

rest - 将 SparkPost 与 XHR 结合使用时出现 401 错误

php - Magento Rest api 通过电子邮件 ID 获取客户

REST API - 命名约定 - 返回对象的集合,但仅返回这些项目的单个属性

string - 在 HTTP 请求的上下文中,一个 255 字节的字符串有多长?

javascript - 使用 Google Apps 脚本运行简单的 HTTP 请求脚本时出现 "Attribute provided with no value"

node.js - 从快速响应中的内容类型中删除字符集

java - 如何使用 RESTEasy 客户端将列表作为查询参数发送

javascript - 在 javascript 自己的库中创建多部分/表单数据请求

javascript - 如何在 Node 中测试请求对象的属性

ios - 尝试使用 Alamofire 4 和 OAuth2 发出 POST 请求时出错 - Swift