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

Scott Werner scott.werner.vt at gmail.com
Thu Jul 27 15:14:43 EDT 2017


Tom,

Request headers key names are case-insensitive, so X-Real-Ip and X-REAL-IP
would both work. I could not repeat your issue using Python 3.6:

from flask import Flask, jsonify, request

app = Flask(__name__)

@app.route('/')
def hello_world():
    print(request.headers)
    print(request.headers.get('Accept', 'No Accept'))
    print(request.headers.get('X-Real-Ip', 'No X-Real-Ip'))
    print(request.headers.get('X-REAL-IP', 'No X-Real-Ip'))
    return jsonify(dict(request.headers))

app.run()


$ curl --header "X-Real-Ip: 8.8.8.8" http://127.0.0.1:5000/
{
  "Accept": "*/*",
  "Host": "127.0.0.1:5000",
  "User-Agent": "curl/7.54.1",
  "X-Real-Ip": "8.8.8.8"
}


127.0.0.1 - - [27/Jul/2017 15:12:18] "GET / HTTP/1.1" 200 -
Host: 127.0.0.1:5000
User-Agent: curl/7.54.1
Accept: */*
X-Real-Ip: 8.8.8.8


*/*
8.8.8.8
8.8.8.8



On Thu, Jul 27, 2017 at 11:53 AM, Tom Vaughan <
thomas.david.vaughan at gmail.com> wrote:

> 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?
> _______________________________________________
> Flask mailing list
> Flask at python.org
> https://mail.python.org/mailman/listinfo/flask
>



-- 
Scott Werner
scott.werner.vt at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20170727/1a1fb3d6/attachment.html>


More information about the Flask mailing list