如果提交的表单包含验证错误,我是否应该返回 400 错误?
Should I respond with a 400 error if a form submit contains validation errors?
在经典的基于表单的网络应用程序中,如果用户提交包含验证错误的 HTML 表单,假设没有 JavaScript,正确的做法是什么?
- 响应 HTTP 200 + 页面内容(包括用户的错误信息)
- 响应 HTTP 400 + 页面内容(包括用户的错误信息)
重要吗?
您的应用正在与人对话,而不是与其他机器对话。因此,您应该做正确的事并以用户友好的方式处理异常。
您的用户不关心 HTTP return 代码,因此您也不应该考虑它。您将业务逻辑问题与 HTTP 协议问题混淆了。
事实上,通过向网络浏览器抛出 400 错误,您很可能只会遇到网络浏览器向用户抛出难看的消息。
如果您编写的是 REST api,那么答案会有所不同。但你不是。
1) 是正确的方法,因为您希望向用户显示一个突出显示无效输入值的内容页面。
2) 的问题在于某些浏览器可能会显示自己的 'friendly' 错误页面,旨在帮助用户理解 4xx 错误。以下是有关 IE 何时显示 'friendly' 个错误页面的一些信息:
一方面,如果它是供人类使用的 Web 应用程序,带有一些有用的错误消息的 200 将起作用。从这个意义上说,为人类制作网站更容易,因为他们可以阅读和理解内容,而不必依赖状态代码来与应用程序交互。
另一方面,如果您考虑 REST API 更合适的是抛出 4xx 错误,因为它是客户端错误。在这种情况下,您有多种选择。
根据RFC2616,400表示
The request could not be understood by the server due to malformed
syntax. The client SHOULD NOT repeat the request without
modifications.
这似乎不合适,因为它不是由于语法格式错误造成的。
但是,RFC2616 is now obsoleted by RFC7230-7235. The new RFC7231 以更广泛的方式定义了 400 的含义。
Client Error 4xx The 4xx (Client Error) class of status code indicates
that the client seems to have erred. Except when responding to a HEAD
request, the server SHOULD send a representation containing an
explanation of the error situation, and whether it is a temporary or
permanent condition.
400 Bad Request
The 400 (Bad Request) status code indicates that the server cannot or
will not process the request due to something that is perceived to be
a client error (e.g., malformed request syntax, invalid request
message framing, or deceptive request routing)
所以这似乎是可以接受的,尽管仍然是通用的。另一种选择是使用 422 status code defined by RFC4918 (WebDAV).
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.
在经典的基于表单的网络应用程序中,如果用户提交包含验证错误的 HTML 表单,假设没有 JavaScript,正确的做法是什么?
- 响应 HTTP 200 + 页面内容(包括用户的错误信息)
- 响应 HTTP 400 + 页面内容(包括用户的错误信息)
重要吗?
您的应用正在与人对话,而不是与其他机器对话。因此,您应该做正确的事并以用户友好的方式处理异常。
您的用户不关心 HTTP return 代码,因此您也不应该考虑它。您将业务逻辑问题与 HTTP 协议问题混淆了。
事实上,通过向网络浏览器抛出 400 错误,您很可能只会遇到网络浏览器向用户抛出难看的消息。
如果您编写的是 REST api,那么答案会有所不同。但你不是。
1) 是正确的方法,因为您希望向用户显示一个突出显示无效输入值的内容页面。
2) 的问题在于某些浏览器可能会显示自己的 'friendly' 错误页面,旨在帮助用户理解 4xx 错误。以下是有关 IE 何时显示 'friendly' 个错误页面的一些信息:
一方面,如果它是供人类使用的 Web 应用程序,带有一些有用的错误消息的 200 将起作用。从这个意义上说,为人类制作网站更容易,因为他们可以阅读和理解内容,而不必依赖状态代码来与应用程序交互。
另一方面,如果您考虑 REST API 更合适的是抛出 4xx 错误,因为它是客户端错误。在这种情况下,您有多种选择。
根据RFC2616,400表示
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.
这似乎不合适,因为它不是由于语法格式错误造成的。
但是,RFC2616 is now obsoleted by RFC7230-7235. The new RFC7231 以更广泛的方式定义了 400 的含义。
Client Error 4xx The 4xx (Client Error) class of status code indicates that the client seems to have erred. Except when responding to a HEAD request, the server SHOULD send a representation containing an explanation of the error situation, and whether it is a temporary or permanent condition.
400 Bad Request
The 400 (Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing)
所以这似乎是可以接受的,尽管仍然是通用的。另一种选择是使用 422 status code defined by RFC4918 (WebDAV).
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.