我已经看到了支持经过身份验证的用户的魔术id的URI,如下所示:
GET /user/me - list my profile
GET /user/me/photos - list my photos
下面的使用实际的用户ID
GET /user/742924
GET /user/742924/photos
我看到的问题是,根据身份验证的用户,相同的资源ID指向不同的资源。
这违反任何REST原则吗?
最佳答案
从REST的 Angular 来看,使用/me
命名与已认证用户相对应的资源是完全可以的。根据Roy Thomas Fielding的论文,可以命名的任何信息都可以是资源:
5.2.1.1 Resources and Resource Identifiers
The key abstraction of information in REST is a resource. Any information that can be named can be a resource: a document or image, a temporal service (e.g. "today's weather in Los Angeles"), a collection of other resources, a non-virtual object (e.g. a person), and so on. In other words, any concept that might be the target of an author's hypertext reference must fit within the definition of a resource. A resource is a conceptual mapping to a set of entities, not the entity that corresponds to the mapping at any particular point in time. [...]
使用
/me
时,您有一个用于身份验证用户的定位器,它将始终标识身份验证用户的概念,而不管哪个用户被身份验证。
关于web-services - 使用魔术(我/自己)资源标识符是否违反REST原则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35719797/