我知道缺少 content-length header 是 411,但不确定当 content-length 中给出的值与实际内容长度不匹配时我应该如何响应
最佳答案
标准似乎没有提及响应的实际代码,只是两个长度必须匹配并且 HTTP/1.1 代理必须通知用户。
我只会使用 400 Bad Request
代码,因为毕竟那是问题所在(违反标准的请求)并且其他 400 系列代码似乎都不够接近匹配。 x00
代码还定义了状态的类,因此可用于通用代码。
请记住,代码是可扩展的,您始终可以定义自己的代码,并且无论如何都需要不识别它的代理将其视为 x00
变体(第 6.1.1 节) 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.
关于http - 内容长度与实际内容长度不匹配的适当 HTTP 状态代码是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6287154/