Flask/Eve + WSGI 和 HTTP_X_HTTP_METHOD_OVERRIDE

Flask/Eve + WSGI and HTTP_X_HTTP_METHOD_OVERRIDE

我正在尝试了解 WSGI 环境 HTTP Header(s) 如何以及何时在应用的请求中重命名 object。

我正在尝试 Eve,我正在发送 POST 或带有 X-HTTP-Method-Override 的 PUT。

Eve 中的代码正在尝试使用以下代码 (here) 访问请求 headers:

return request.headers.get('X-HTTP-Method-Override', request.method)

在我的 WSGI 环境中,我有一个 HTTP_X_HTTP_METHOD_OVERRIDE,其值为 PATCH。

当我尝试执行 request.headers 转储时,我得到:

Request Header: ('X-Http-Method-Override', u'PATCH')
Request Header: ('Origin', u'http://localhost:9000')
Request Header: ('Content-Length', u'622')
Request Header: ('Host', u'localhost:24435')
Request Header: ('Accept', u'application/json;charset=UTF-8')
Request Header: ('Content-Type', u'application/json')
Request Header: ('Accept-Encoding', u'identity')

我在网上查过,其他 Python 应用程序正在尝试访问此特定请求 header 案例:

X-HTTP-Method-Override 而不是 X-Http-Method-Override(我在请求中得到)

Flask 负责为您从 WSGI 环境变量中提取 headers,在此过程中删除初始的 HTTP_ 前缀。该前缀在 WSGI 环境中用于区分 header 与其他 WSGI 信息,但是一旦您将 header 提取到专用结构中,该前缀就完全多余了。

request object 还为您提供了专门的字典,其中的键不区分大小写。只要小写的版本与小写的 header 键匹配,这里使用什么大小写都没有关系; httpHttpHTTPHtTp 都是有效的大小写变体。这是因为 HTTP 标准明确规定在处理 headers.

时应忽略大小写

查看Headers class reference in the Werkzeug documentation, it is the bases for the request.headers object. It in turn is compatible with the wsgiref.headers.Headers class,包括:

For each of these methods, the key is the header name (treated case-insensitively), and the value is the first value associated with that header name.

强调我的。