gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 15:12:23 24 4
gpt4 key购买 nike

我只花了 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 serverunderstands the content type of the request entity (hence a415(Unsupported Media Type) status code is inappropriate), and thesyntax of the request entity is correct (thus a 400 (Bad Request)status code is inappropriate) but was unable to process the containedinstructions. For example, this error condition may occur if an XMLrequest body contains well-formed (i.e., syntactically correct), butsemantically 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 requiredto understand the meaning of all registered status codes, though suchunderstanding is obviously desirable. However, applications MUSTunderstand the class of any status code, as indicated by the firstdigit, and treat any unrecognized response as being equivalent to thex00 status code of that class, with the exception that anunrecognized response MUST NOT be cached. For example, if anunrecognized status code of 431 is received by the client, it cansafely assume that there was something wrong with its request andtreat the response as if it had received a 400 status code. In suchcases, user agents SHOULD present to the user the entity returnedwith 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com