REST API设计:它是确定一个PUT通话过程中改变资源标识符? [英] REST API Design : Is it ok to change the resource identifier during a PUT call?

查看:143
本文介绍了REST API设计:它是确定一个PUT通话过程中改变资源标识符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,更多地了解周围的PUT调用REST风格的设计模式。具体来说,我是通过改变资源ID作为PUT调用的一部分违反规范?

I'm curious to learn more about RESTful design patterns around the PUT call. Specifically, am I violating norms by changing the resource ID as part of a PUT call?

考虑以下...

POST /api/event/  { ... } - returns the resource ID (eventid) of the new event in the body
GET  /api/event/eventid
PUT  /api/event/eventid   - returns the (possibly new) resource ID depending on request body
GET  /api/event/eventid   - fails if the original eventid was used in the URI

有关GET和PUT端点可以快速访问资源,如果事件ID重新presents内部资源(如数据库记录)。如果在服务器中的PUT结果移动底层的资源,该ID可以改变

The endpoints for GET and PUT can quickly access the resource if the eventid represents internal resources (like a database record). If the PUT results in the server moving the underlying resource, the ID can change.

我是不是违反规范,当我做到这一点?

Am I violating norms when I do this?

推荐答案

REST是不是一个严格的规范,但更多的是一套指导方针和最佳实践可以遵循,以构建Web服务,这是很容易理解和使用工作。因此,有没有什么prevents你从一个PUT期间更改资源ID。

REST is not a strict specification, but more a set of guidelines and best practices that can be followed to build web-services that are easy to understand and work with. So there's nothing that prevents you from changing a resource IDs during a PUT.

话虽这么说,这样做是IMO一种不好的做法。一个REST背后的思想是,每个资源都可以使用 URI 引用。你的情况,这是URI路径和(我认为)内部ID的串联。这个URI可以由其他系统被用来与作为参考存储。如果你改变一个把资源的ID,你改变URI和该资源的所有引用将被打破(404)。

That being said, doing so is IMO a bad practice. One of the ideas behind REST is that each resource can be referenced using a URI. In your case this URI is the concatenation of the path and (I assume) an internal ID. This URI could be used by other "systems" and stored as references. If you change the ID of a resource on a PUT, you change the URI and all references to that resource will be broken (404).

如果你觉得有必要改变这种状况是URI的一部分ID,您可能没有采摘的权利属性。考虑别的东西,这将是不可变的(如:用 UUID 标记你的资源,并使用它,而不是一个内部数据库ID)

If you feel the need to change the ID that is part of the URI, you may not have picked the right property for it. Consider something else that would be immutable (e.g.: tag your resource with a UUID and use it rather than an internal DB ID).

这篇关于REST API设计:它是确定一个PUT通话过程中改变资源标识符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆