http - 我应该使用 HTTP 4xx 来指示 HTML 表单错误吗?

标签 http

我只花了 20 分钟调试一些 (django) 单元测试。我正在测试一个 View POST,我期待一个 302 返回代码,之后我断言一堆数据库实体符合预期。原来最近合并的提交添加了一个新的表单字段,我的测试失败了,因为我没有包含正确的表单数据。

问题是测试失败了,因为 HTTP 返回代码是 200,而不是 302,我只能通过打印响应 HTTP 并查看它来解决问题。除了必须通过 HTML 来解决问题的烦恼之外,200 似乎是未处理的 POST 的错误代码。 4xx(客户端错误)似乎更合适。此外,它会使调试测试变得轻而易举,因为响应代码会直接指出问题所在。

我读过有关在 REST API 中使用 422(无法处理的实体)作为可能的返回码的信息,但找不到在 HTML View /处理程序中使用它的任何证据。

我的问题是 - 是否还有其他人这样做,如果没有,为什么不这样做?

[更新 1]

澄清一下,这个问题涉及 HTML 表单,而不是 API。

这也是一个关于 HTTP 响应代码本身的问题 - 而不是 Django。这恰好是我正在使用的。我已经删除了 django 标签。

[更新 2]

一些进一步的说明,W3C 引用 ( http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html ):

10.2 Successful 2xx

This class of status code indicates that the client's request was successfully received, understood, and accepted.

10.4 Client Error 4xx

The 4xx class of status code is intended for cases in which the client seems to have erred.

10.4.1 400 Bad Request

The request could not be understood by the server due to malformed syntax.

来自https://www.rfc-editor.org/rfc/rfc4918#page-78

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.

[更新 3]

深入研究,422 是一个 WebDAV 扩展[1],这可以解释它的晦涩难懂。也就是说,由于 Twitter 将 420 用于他们自己的目的,我想我会随心所欲。但它将以 4 开头。

[更新 4]

有关使用自定义响应代码以及应如何处理它们(如果无法识别)的说明,来自 HTTP 1.1 规范 (https://www.rfc-editor.org/rfc/rfc2616#section-6.1.1):

HTTP status codes are extensible. HTTP applications are not required to understand the meaning of all registered status codes, though such understanding is obviously desirable. However, applications MUST understand the class of any status code, as indicated by the first digit, and treat any unrecognized response as being equivalent to the x00 status code of that class, with the exception that an unrecognized response MUST NOT be cached. For example, if an unrecognized status code of 431 is received by the client, it can safely assume that there was something wrong with its request and treat the response as if it had received a 400 status code. In such cases, user agents SHOULD present to the user the entity returned with the response, since that entity is likely to include human- readable information which will explain the unusual status.

[1] https://www.rfc-editor.org/rfc/rfc4918

最佳答案

你是对的,如果结果不成功,200 是错误的。

我还认为 success-with-redirect-to-result-page 应该是 303,而不是 302。

4xx 对于客户端错误是正确的。 422对我来说似乎是正确的。无论如何,不​​要在未通过 IANA 注册的情况下发明新的 4xx 代码。

关于http - 我应该使用 HTTP 4xx 来指示 HTML 表单错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15580969/

相关文章:

http - Spring RestTemplate - 带请求正文的 http GET

ruby - gitlab 通过 api 创建新用户会产生 404

java - Java 中的 HTTP 编程?

ruby - 通过 302 重定向维护 session 和 cookie

arrays - RxJS 限制和返回响应结果数组的最佳方式

php - 是否有任何现有服务可通过浏览器上传巨大的视频文件 (~5gb)?

c# - 使用 POST 请求的简单 Web 服务器 - 服务器在一两个请求后挂起

api - ember-data REST 服务器 API 应该返回什么?

api - 为 kml 创建一个 http 地址

android - 如何在 python Flask 服务器端接收从 android 客户端上传的文件