PUT URI 的 id 不同于 body 的 id
PUT with id of the URI different from the id of the body
当客户端尝试更新资源时,如果 URI 的 id 与正文的 id 不同,应该如何响应?
例如:
URI :
PUT /members/123
正文
{
id : 456,
name : "john"
}
https://www.rfc-editor.org/rfc/rfc7231#section-4.3.4
An origin server SHOULD verify that the PUT representation is consistent with any constraints the server has for the target resource that cannot or will not be changed by the PUT. This is particularly important when the origin server uses internal configuration information related to the URI in order to set the values for representation metadata on GET responses. When a PUT representation is inconsistent with the target resource, the origin server SHOULD either make them consistent, by transforming the representation or changing the resource configuration, or respond with an appropriate error message containing sufficient information to explain why the representation is unsuitable. The 409 (Conflict) or 415 (Unsupported Media Type) status codes are suggested, with the latter being specific to constraints on Content-Type values.
正文中的 id 应与资源标识符相匹配的要求(或等效地,资源标识符不可变的要求)将算作对资源的约束。因此,如果是这种情况,您应该保留 entire 资源不变,并且 return 带有错误消息的 409。
也就是说,标识符和表示需要具有任何共同数据并没有特别的原因。想想 hashmap/dictionary/key-value 商店。将状态 id:456
存储在键 /members/123
下没有任何问题。如果这在你的资源模型中是合适的,那么将新的表示放入商店和 return 200.
当客户端尝试更新资源时,如果 URI 的 id 与正文的 id 不同,应该如何响应? 例如:
URI :
PUT /members/123
正文
{
id : 456,
name : "john"
}
https://www.rfc-editor.org/rfc/rfc7231#section-4.3.4
An origin server SHOULD verify that the PUT representation is consistent with any constraints the server has for the target resource that cannot or will not be changed by the PUT. This is particularly important when the origin server uses internal configuration information related to the URI in order to set the values for representation metadata on GET responses. When a PUT representation is inconsistent with the target resource, the origin server SHOULD either make them consistent, by transforming the representation or changing the resource configuration, or respond with an appropriate error message containing sufficient information to explain why the representation is unsuitable. The 409 (Conflict) or 415 (Unsupported Media Type) status codes are suggested, with the latter being specific to constraints on Content-Type values.
正文中的 id 应与资源标识符相匹配的要求(或等效地,资源标识符不可变的要求)将算作对资源的约束。因此,如果是这种情况,您应该保留 entire 资源不变,并且 return 带有错误消息的 409。
也就是说,标识符和表示需要具有任何共同数据并没有特别的原因。想想 hashmap/dictionary/key-value 商店。将状态 id:456
存储在键 /members/123
下没有任何问题。如果这在你的资源模型中是合适的,那么将新的表示放入商店和 return 200.