418“我是茶壶”真的是HTTP响应代码吗?

在互联网上有各种各样的参考,包括在响应代码列表中,但我不知道这是否是一个奇怪的笑话。


HTTP响应代码418最初是在RFC 2324(“超文本咖啡壶控制协议(HTCPCP/1.0)”)和RFC 7168(“茶叶外排设备超文本咖啡壶控制协议(HTCPCP- Tea)”)协议中定义的。

根据维基百科:HTTP状态代码列表:#418

这段代码在1998年被定义为传统的IETF愚人节玩笑之一,在RFC 2324(超文本咖啡壶控制协议)中,预计不会被实际的HTTP服务器实现。RFC指定该代码应由用于冲泡咖啡的茶壶返回。这个HTTP状态在一些网站(包括Google.com)中被用作彩蛋。

我使用这个代码。我有nginx反向代理请求到两个不同的HTTP服务器。一个处理未经身份验证的用户的请求,另一个处理经过身份验证的用户的请求。在这个特殊情况下的问题是,第一个服务器是决定用户是否经过身份验证的服务器。请不要问为什么。

因此,如果第一个服务器确定用户已通过身份验证,它将响应418 I'm a teapot。NGINX然后在内部将流量重新路由到第二台服务器。就浏览器而言,这是一个单独的请求。

这符合HTCPCP代码418的精神,因为如果您试图使用茶壶进行BREW,则适当的响应是“我不是那种可以处理该请求的人,但可能还有其他请求。”. .换句话说,“我是茶壶。找个咖啡机。”(第二个服务员是咖啡机)。

最终,虽然418在RFC 7231中没有明确定义,但它仍然被4xx(客户端错误)的保护伞所覆盖。

6. Response Status Codes 4xx (Client Error): The request contains bad syntax or cannot be fulfilled 6.5. 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. These status codes are applicable to any request method. User agents SHOULD display any included representation to the user.

我认为把418当作一个曾经有半官方含义,但现在正式“未分配”的保留代码是更安全的。

我认为,历史上对这些代码的看法与现在有所不同。这在今天听起来毫无意义,也很有趣;也许不是?

换句话说,我将避免使用此代码。

是的,我可以确认,我已经看到HTTP 418从真正的生产服务器返回。它确实存在。

Yes it's a "real" code in the sense that it was published as part of an official RFC by the Internet Engineering Task Force, RFC-2324. However, since that RFC was published on April 1 and meant as an April fools joke (along with the rest of Hyper Text Coffee Pot Control Protocol), not for legitimate implementation, it's not a "serious" code in the typical sense*. That's why most sites use it as an Easter egg, but otherwise avoid it. As noted by wizulus in this comment, there are often more appropriate statuses like 400 (Bad Request). Despite its satirical nature, it's now a reserved code (presumably as a result of becoming a popular engineering meme for the briefest moment), so don't expect it to be going anywhere anytime soon.

*根据RFC的作者Larry M Masinter的说法,问题中的HTTP扩展实际上有一个严肃但讽刺的目的:“它确定了HTTP扩展不恰当的许多方式。”

你真的不应该用超文本茶壶控制协议(HTTCP)来煮咖啡。这是HTCPCP(超文本咖啡壶控制协议)的工作。

我真的在用它。在我的项目中,我们有一个后端和一个前端,如果我正在开发一个新的API,我使用418来表示“不应该在前端做出的坏请求”。它们现在在我们的错误报告工具中触发一个严重级别为“警告”的事件,而标准400只在级别为“信息”的情况下触发。

我不想使用500,因为它是一个错误的调用者,我不认为它是一个常规的400,因为我们有很多情况下,后端是处理验证和400不是一个bug。我们本可以使用501,但它在4xx中,因为它是一个请求错误。