失败的POST请求使用什么HTTP响应代码? [英] What HTTP response code to use for failed POST request?

查看:235
本文介绍了失败的POST请求使用什么HTTP响应代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当POST请求失败并且请求主体的格式正确时,应该返回什么HTTP响应代码?

What HTTP response code should be returned when a POST request was not successful and a request body was correctly formatted?

对于成功的POST请求,我使用的是201-已创建,但是没有等效的未创建代码.

For successful POST request i am using 201 - Created, but there is no equivalent not created code.

我在考虑400-错误的请求,但实际上会向用户指出请求的格式不正确或304-未修改.

I am thinking either 400 - bad request but that would actually point user that a request is poorly formatted or 304 - not modified.

推荐答案

POST请求不成功且请求主体的格式正确时,应返回什么HTTP响应代码?

What HTTP response code should be returned when a POST request was not successful and a request body was correctly formatted?

如果您表示请求有效内容的语法有效,但是由于数据无效而无法进行处理,则可以使用 422 :

If you mean the syntax of the request payload is valid but it cannot be processed due to invalid data, you can use 422:

11.2. 422无法处理的实体

422(不可处理实体)状态代码表示服务器 了解请求实体的内容类型(因此 415(不受支持的媒体类型)状态代码不合适),并且 请求实体的语法正确(因此400(错误请求) 状态代码不正确),但无法处理其中包含的内容 指示.例如对于如果XML可能会出现此错误情况 请求正文包含格式正确(即语法正确)的内容,但 语义错误的XML指令.

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 .

Remember to provide a good description in the response payload explaining what's wrong with the payload. Refer to the RFC 7807 for details on how to report problems in HTTP APIs.

POST请求失败的原因更多是业务逻辑错误,例如帐​​户余额太低,无法提取5.00美元" .

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".

对于 403 409 会更合适.

For the situation described in your comment, 403 or 409 would be a better fit.

6.5.3. 403禁止

403(禁止)状态代码表示服务器已理解 该请求,但拒绝授权.希望执行以下操作的服务器 公开为什么禁止请求的原因可以描述为 响应有效负载中的原因(如果有). [...]

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). [...]

6.5.8. 409冲突

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. [...]

这篇关于失败的POST请求使用什么HTTP响应代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆