在我的情况下,我将允许用户使用 url 中的代码(例如:apidomain/{code})更新资源,也可以通过在模型中传递代码来更改代码。
如果我允许我的用户在 PUT 调用期间更改资源的资源标识符,我是否偏离了 REST 模式?
HTTP PUT
means "put the body of my request here", where the URL at which you make the request is where to put the entity. If you PUT
a resource somewhere, you should be able to GET
it again.
If the content of the request means that the resource will not be found at the URL you just PUT
it at, you've broken one of the HTTP constraints.
I can think of a couple of options to handle this scenario which would all fit within HTTP's constraints:
PUT
of the resource to its new URL. The PUT
should contain enough information to recognise the resource already exists at a previous location and to internally perform a "relocate" by changing the relevant ID.DELETE
the first resource and PUT
the resource with its new ID in its new location. You might be able to track some history between the two internally, if required.However you decide to perform a "relocation" of a resource, you should make it so anybody attempting to GET
the old resource should receive a 301 Moved Permanently response, which should include the new location of the resource.