REST的资源和操作URI约定 [英] Resource and Action URI convention for REST
问题描述
我正处于开发一些REST API的十字路口,而且我没有找到关于这个主题的真正讨论,更不用说对这两者的坚定辩护了。
I am at a cross roads for developing some REST APIs and I have found no real discussion on the subject much less a firm defense of either.
这是我的理解使用REST,你有 /< resource> /< action>
作为你的网址
It's my understand that with REST you have /<resource>/<action>
as your URL
所以,要禁用您将拥有的用户:
So, to disable a user you would have:
PUT /user/disable
似乎合理。但是,我们正在讨论更基本的方法:
Seems reasonable. However, we have the debate going on out more basic methods:
应该是:
POST /user (creates a user *implicitly*)
POST /user/create (creates a user *explicitly*)
DELETE /user/:id
DELETE /user/:id/delete
第一个似乎被认为是标准 第二个显然更明确的意图,并与/ user / disable等方法一致
The first seems to be what is considered "the standard" and the second is obviously much more clear in it's intent and is consistent with methods like /user/disable
也许这场辩论在其他地方肆虐,但我还没有看到它。如果你对此有'宗教信仰',现在你有机会进行辩护
Maybe this debate has raged elsewhere but I have not seen it. If you're 'religious' about this, now's your chance to pontificate
推荐答案
拥有 /<资源> /< action>
因为您的网址不是REST。 REST使用HTTP谓词来确定要对给定资源或资源集合执行的操作。这意味着:
Having /<resource>/<action>
as your URL is not REST. REST uses HTTP verbs to determine actions to perform on a given resource, or on a collection of resources. That means:
- 创建资源是:
POST / users
- 阅读资源是:
GET / users /< id>
- 更新(实际替换)a资源是:
PUT / users /< id>
- 删除资源是:
DELETE / users / < id>
- Creating a resource is:
POST /users
- Reading a resource is:
GET /users/<id>
- Updating (replacing actually) a resource is:
PUT /users/<id>
- Deleting a resource is:
DELETE /users/<id>
这是REST。
对于所有其他方法,您可以使用其他动词,如 PATCH
。禁用用户可能是:
For all other methods, you can use other verbs like PATCH
. Disabling a user could be:
PATCH /users/<id>/disable
这不是纯粹的REST,但没关系。
It's not pure REST but it's ok.
编辑:如果您希望与REST兼容,则禁用用户意味着您要更改其状态。换句话说,您想要更改其中一个属性,如州
标志。您可以修补您的资源:
If you want to be REST compliant, disabling a user means you want to change his state. In other words, you want to change one of its properties like a state
flag. You can "patch" your resource:
PATCH /users/<id>?state=disabled
这是REST。您还可以使用 PUT
替换资源,如评论中所述。
This is REST. You can also replace the resource by using PUT
as described in the comments.
这篇关于REST的资源和操作URI约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!