[Flask] Cannot get custom header value from request.headers

Tom Vaughan thomas.david.vaughan at gmail.com
Thu Jul 27 11:53:40 EDT 2017


On Thu, Jul 27, 2017 at 12:51 AM, Tom Vaughan
<thomas.david.vaughan at gmail.com> wrote:
> Hi,
>
> Why would this:
>
>     print(request.headers)
>     print(request.headers.get('Accept', 'No Accept'))
>     print(request.headers.get('X-Real-Ip', 'No X-Real-Ip'))
>
> cause this output:
>
>     Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
>     Connection: keep-alive
>     X-Real-Ip: 172.17.0.1
>     [...]
>
>     text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
>     No X-Real-Ip
>
> ?

So the problem is:

    File "/usr/local/lib/python3.5/dist-packages/werkzeug/datastructures.py",
line 1349, in __getitem__

        key = key.upper().replace('-', '_')
        if key in ('CONTENT_TYPE', 'CONTENT_LENGTH'):
            return _unicodify_header_value(self.environ[key])
        return _unicodify_header_value(self.environ['HTTP_' + key])

The problem is that the custom header values (those that start with
X-) are not capitalized in self.environ, everything else is. For
example in self.environ, Accept exists as HTTP_ACCEPT, but X-Real-Ip
exists as HTTP_X_Real_IP. But why?


More information about the Flask mailing list