1

在我的情况下,我将允许用户使用 url 中的代码(例如:apidomain/{code})更新资源,也可以通过在模型中传递代码来更改代码。

如果我允许我的用户在 PUT 调用期间更改资源的资源标识符,我是否偏离了 REST 模式?

4

1 回答 1

0

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:

  1. Make it so that the URL of the resource is immutable. The "real" ID of any resource in a RESTful system is its URL. If you want to have an ID which is mutable, make it a surrogate key supported by an immutable identifier which is used to build the URL, or just make the URL the primary key.
  2. Remove the ID of the entity from its representation (so there can be no confusion around identifiers) and perform a 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.
  3. 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.

于 2013-07-23T09:51:12.007 回答