我在构建 RESTful API 时遇到过一种情况。在用户注册期间,如果电子邮件已经存在,那么 422
和 409
之间哪个 http 响应代码有意义?
我浏览过类似的 one并且接受的答案来自 2012 年。答案仍然有效吗?例子会有很大帮助。
最佳答案
你可能找不到这个问题的非常明确的答案,一旦两者409
和 422
适合这种情况(不过我会选择 409
)。
对于其中任何一个,您必须确保将描述问题的有效负载发送回客户端。
The
409
(Conflict) status code indicates that the request could not be completed due to a conflict with the current state of the target resource. This code is used in situations where the user might be able to resolve the conflict and resubmit the request. The server SHOULD generate a payload that includes enough information for a user to recognize the source of the conflict. [...]
11.2. 422 Unprocessable Entity
The
422
(Unprocessable Entity) status code means the server understands the content type of the request entity (hence a415
(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a400
(Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous, XML instructions.
关于rest - 注册期间现有电子邮件的 422 或 409 状态代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50946698/