当在 URL 和 body 中使用 id 进行 PUT 时,哪个获胜?

When PUTting with an id in the URL and in the body, which one wins?

尝试找出以下情况下的正确 restful 行为:

这些是对我有吸引力的选项,按降序排列:

RFC 7231

A successful PUT of a given representation would suggest that a subsequent GET on that same target resource will result in an equivalent representation being sent in a 200 (OK) response.

资源的标识符及其表示不需要以任何语义上重要的方式一致,就像文件的完全限定路径不需要与其内容一致一样,大致相同字典中的值不需要与其键一致的方式。

GET /objects/1

200 OK

{ "id": 2 }

就 HTTP 而言,这是完全有效的,就像

GET /fbb477c8-e2f3-4581-9c44-27305de6ebee

200 OK

{ "id": 2 }

您需要依赖的标准的关键部分是:

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.

完全符合规则的是,PUT/fbb477c8-e2f3-4581-9c44-27305de6ebee 的任何表示都必须大约 id:1,并拒绝不满足该不变量的不一致请求。 409 Conflict 是在这种情况下使用的常用状态代码。

当然,标识符 /objects/1 也是如此。