失败的 POST 请求使用什么 HTTP 响应代码?
What HTTP response code to use for failed POST request?
当 POST 请求不成功且请求正文格式正确时,应返回什么 HTTP 响应代码?
对于成功的 POST 请求,我使用 201 - 已创建,但没有等效的未创建代码。
我在想要么是 400 - 错误的请求,但实际上会告诉用户请求格式不正确,要么是 304 - 未修改。
What HTTP response code should be returned when a POST
request was not successful and a request body was correctly formatted?
如果你的意思是请求有效负载的语法有效但由于数据无效而无法处理,你可以使用422
:
11.2. 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 an XML
request body contains well-formed (i.e., syntactically correct), but
semantically erroneous, XML instructions.
记得在响应负载中提供一个很好的描述,解释负载有什么问题。有关如何在 HTTP API 中报告问题的详细信息,请参阅 RFC 7807。
更新(根据评论)
The reason why a POST
request would fail is more of a business logic error, for example "account balance too low to withdraw 5.00 USD".
The 403
(Forbidden) status code indicates that the server understood
the request but refuses to authorize it. A server that wishes to
make public why the request has been forbidden can describe that
reason in the response payload (if any). [...]
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. [...]
当 POST 请求不成功且请求正文格式正确时,应返回什么 HTTP 响应代码?
对于成功的 POST 请求,我使用 201 - 已创建,但没有等效的未创建代码。
我在想要么是 400 - 错误的请求,但实际上会告诉用户请求格式不正确,要么是 304 - 未修改。
What HTTP response code should be returned when a
POST
request was not successful and a request body was correctly formatted?
如果你的意思是请求有效负载的语法有效但由于数据无效而无法处理,你可以使用422
:
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.
记得在响应负载中提供一个很好的描述,解释负载有什么问题。有关如何在 HTTP API 中报告问题的详细信息,请参阅 RFC 7807。
更新(根据评论)
The reason why a
POST
request would fail is more of a business logic error, for example "account balance too low to withdraw 5.00 USD".
The
403
(Forbidden) status code indicates that the server understood the request but refuses to authorize it. A server that wishes to make public why the request has been forbidden can describe that reason in the response payload (if any). [...]
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. [...]