是否允许使用 409 Conflict 状态码来防止某些字段在 HTTP PUT 请求中被更新

Is it allowed to use the 409 Conflict status code to prevent some fields from being updated in a HTTP PUT request

在处理 (json) REST API 时,我遇到了这样一种情况,即我们有一个 user 资源的表示,我想在其中阻止一个字段 (email) 在 PUT 中被覆盖。如果我理解正确,那么 PUT 应该包含资源的完整表示以替换旧的。

因此,例如,当从 API 中获取用户时(为简单起见,省略了一些 headers):

> GET /user/123 HTTP/1.1
> Host: example.com
> Authorization: Bearer XXXXX
> Accept: application/json

< HTTP/1.1 200 OK
< Content-Type: application/json
< 
< { "name": "John Smiht"
< , "email": "john.smith@example.com"
< }

如果您想更正 John Smith 姓名中的拼写错误,您会这样做:

> PUT /user/123 HTTP/1.1
> Host: example.com
> Content-Type: application/json
> 
> { "name": "John Smith"
> , "email": "john.smith@example.com"
> }

< HTTP/1.1 201 No Content

现在,如果有人要输入不同的 e-mail 地址,我可以使用 409 来表示请求未被处理吗?

> PUT /user/123 HTTP/1.1
> Host: example.com
> Content-Type: application/json
> 
> { "name": "John Smith"
> , "email": "something.else@example.com"
> }

< HTTP/1.1 409 Conflict
< Content-Type: application/json
<
< { "errorNumber": "XXX"
< , "errorMessage": "Not allowed to change e-mail address this way"
< }

根据https://www.rfc-editor.org/rfc/rfc2616

所以,我的问题是:我应该使用哪个状态码?


编辑:根据已接受的答案;响应将变为

> PUT /user/123 HTTP/1.1
> Host: example.com
> Content-Type: application/json
> 
> { "name": "John Smith"
> , "email": "something.else@example.com"
> }

< HTTP/1.1 403 Forbidden
< Content-Type: application/json
<
< { "errorNumber": "XXX"
< , "errorMessage": "Not allowed to change e-mail address this way"
< }

我会说,如果用户无权更改电子邮件地址,403 将是正确的代码,因为服务器理解用户但拒绝对其采取行动。 (通常这意味着缺少范围/权限)