已经有 HTTP 状态码,为什么还有 HTTP 状态信息?

Why is there a HTTP status message when there already is a HTTP status code?

HTTP 响应可能如下所示:

HTTP/1.1 200 OK

我觉得200已经是告诉客户没问题了,OK的回应可以省略。那么现有的方法是什么?

来自https://greenbytes.de/tech/webdav/rfc7230.html#rfc.section.3.1.2

The reason-phrase element exists for the sole purpose of providing a textual description associated with the numeric status code, mostly out of deference to earlier Internet application protocols that were more frequently used with interactive text clients. A client SHOULD ignore the reason-phrase content.

此类消息称为 原因短语,如 中所述,它仅提供与数字状态代码关联的文本描述,应该被忽略客户端。原因短语甚至可以为空。

虽然您的问题是关于 HTTP/1.1 协议的,但我想强调 HTTP/2 回复不包含任何原因短语。它们仅携带状态代码,如 RFC 7540:

中所定义

For HTTP/2 responses, a single :status pseudo-header field is defined that carries the HTTP status code field. This pseudo-header field MUST be included in all responses; otherwise, the response is malformed.

HTTP/2 does not define a way to carry the version or reason phrase that is included in an HTTP/1.1 status line.