接收单个字段信息的端点的 API 设计?

发布于 2025-01-10 11:31:00 字数 1432 浏览 0 评论 0原文

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

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

发布评论

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

评论(1

ま柒月 2025-01-17 11:31:00

难道我们不能有一个仅接收单个字符串(喜欢/不喜欢)的端点,而不将其封装在 JSON 对象中吗?仍然是RESTful设计吗? (这篇文章表明字符串被视为有效的 JSON)

是的,可以。没有规则规定请求负载必须是对象,甚至没有规定请求负载必须是 JSON。在网络上,许多请求有效负载都是 application/x-www-form-urlencoded 键值对。

权衡是您可以通过添加更多“可选”字段来扩展对象的架构,而不会破坏向后兼容性。


拥有一个仅接收单字段信息(在本例中,实际上是二进制信息:true/false)的 REST 端点,这难道不是一种代码味道吗?

是的 - 从某种意义上说,“气味”表明某处可能存在问题(请参阅https://wiki.c2.com/?CodeSmell)。

文档中只有一个信息字段并没有什么问题。

同样,通过网络提供这样的文档也没有什么问题。支持从网络对该文档进行更改并没有什么问题。

当您将编辑发送到一个资源并期望效果出现在其他地方时,事情会变得有点可疑。例如,如果我们提交

POST /api/great_new_feature/feedback

时期望它会改变我们看到的内容,

GET /api/great_new_feature

那么就值得进一步审查,因为它阻碍了通用目的 HTTP 中内置的缓存失效机制。如果您放弃“免费”的东西,那么您最好确保自己得到了补偿。在代码审查中,我希望找到决策记录 记录调查及其结果。


我不确定我是否理解关于缓存失效的最后一段。它是否特定于需要单个信息字段的 API?

不会。HTTP 消息语义是其统一接口——所有资源都以相同的方式理解消息。


这个问题的普遍接受的解决方案是什么? (我发现我工作的整个网站都有缓存控制:无存储策略)

确保修改信息的请求与读取信息的请求使用相同的目标 URI。

POST /collection

是将新项目引入集合的流行选择,因为(部分)我们期望新项目的引入也会改变集合本身的表示(通常带有指向新项目的链接) 。

Could we not have an endpoint that receives only a single string (like / dislike), without having it encapsulated in a JSON object ? Is it still a RESTful design? (This post suggests that a string is considered valid JSON)

Yes, you can. There is no rule that says that a request payload must be an object, or even that a request payload must be JSON. On the web, many request payloads are application/x-www-form-urlencoded key value pairs.

The trade off is that you can extend the schema of an object by adding more "optional" fields, without breaking backwards compatibility.


Isn’t it a code smell to have a REST endpoint that receives only a single-field information (and, in this case, actually a binary one: true/false)?

Yes - in the sense that a "smell" indicates that there might be a problem somewhere (see https://wiki.c2.com/?CodeSmell ).

There's nothing wrong with having a document that has but a single field of information in it.

Similarly, there's nothing wrong with making such a document available via the web; there's nothing wrong with supporting changes to that document from the web.

Things get a little bit suspicious when you send edits to one resource expecting the effects to show up somewhere else. For example, if we are submitting

POST /api/great_new_feature/feedback

In the expectation that it will change what we see when we

GET /api/great_new_feature

Then that deserves further review, because it impedes the general purpose cache invalidation mechanism built into HTTP. If you are giving up something that is "free", then you better be sure that you are being compensated. In a code review, I would expect to find a decision record documenting the investigation and its findings.


I’m not sure I understand the last paragraph about cache invalidation. Is it specific to an API expecting a single field of information ? 

No. HTTP message semantics are part of its uniform interface -- all resources understand messages the same way.


And what is a generally accepted solution to this problem? (I see that the whole website I work on has a Cache-Control: no-store policy)

Ensure that requests that modify information use the same target URI as the requests that read information.

POST /collection

Is a popular choice for introducing new items into a collection because (in part) we expect that the introduction of the new item also changes the representation of the collection itself (typically with a link to the new item).

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