我应该使用什么 HTTP 状态代码来验证昵称?
What HTTP status code should I use for nickname validation?
我正在构建一个 API,其中一个端点与公司的昵称验证有关。我阅读了很多有关 HTTP 状态代码的文章,对于实体验证,422
似乎是最佳选择。在我的示例中进行一次字段验证怎么样?
What HTTP status code should I use for nickname validation?
For example it already exists
我认为409 Conflict是一个合适的选择
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.
用户 1 选择了一个用户名,用户 2 想要相同的用户名,但不能,因为它与用户 1 的用户名冲突
or has not allowed chars
为此,您提到的 422 Unprocessable Entity 似乎没问题。
The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if request body contains well-formed (i.e., syntactically correct), but semantically erroneous, instructions.
强调我的
我正在构建一个 API,其中一个端点与公司的昵称验证有关。我阅读了很多有关 HTTP 状态代码的文章,对于实体验证,422
似乎是最佳选择。在我的示例中进行一次字段验证怎么样?
What HTTP status code should I use for nickname validation?
For example it already exists
我认为409 Conflict是一个合适的选择
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.
用户 1 选择了一个用户名,用户 2 想要相同的用户名,但不能,因为它与用户 1 的用户名冲突
or has not allowed chars
为此,您提到的 422 Unprocessable Entity 似乎没问题。
The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if request body contains well-formed (i.e., syntactically correct), but semantically erroneous, instructions.
强调我的