内容长度与实际内容长度不匹配的适当 HTTP 状态代码是什么

发布于 2024-11-14 11:03:20 字数 56 浏览 2 评论 0原文

我知道缺少内容长度标头是 411,但不确定当内容长度中给出的值与实际内容长度不匹配时我应该如何响应

I know that missing the content-length header is a 411, but not sure how I should respond when the value given in the content-length does not match the actual content length

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

递刀给你 2024-11-21 11:03:20

该标准似乎没有提及要响应的实际代码,只是两个长度必须匹配并且 HTTP/1.1 代理必须通知用户。

我只会使用 400 Bad Request 代码,因为毕竟这就是问题所在(违反标准的请求),并且其他 400 系列代码似乎都没有足够接近的匹配。 x00 代码还定义状态的,因此可用于通用代码。

请记住,代码是可扩展的,您始终可以定义自己的代码,并且无法识别它的代理无论如何都需要将其视为 x00 变体(RFC2616 的第 6.1.1 节):

HTTP 状态代码是可扩展的。 HTTP 应用程序不需要理解所有已注册状态代码的含义,尽管这种理解显然是可取的。但是,应用程序必须理解任何状态代码的类别(如第一个数字所示),并将任何无法识别的响应视为等同于该类别的 x00 状态代码,但不能缓存无法识别的响应。例如,如果客户端收到无法识别的状态代码 431,则它可以安全地假设其请求出现问题,并将响应视为收到了 400 状态代码。在这种情况下,用户代理应该向用户呈现随响应返回的实体,因为该实体可能包含人类可读的信息来解释异常状态。

The standard seems silent on the actual code to respond with, only that the two lengths MUST match and that HTTP/1.1 agents MUST notify the user.

I would just use the 400 Bad Request code since that is, after all, what the problem is (a request violating the standard) and none of the other 400-series codes seems a close enough match. The x00 codes also define the class of a status so can be used for a generic code.

Keep in mind that the codes are extendable, you can always define your own, and agents that don't recognise it are required to treat it as an x00 variant anyway (section 6.1.1 of RFC2616):

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文