0

I just want to know why django change requests header to uppercase ?

example :

i send headers

"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36" ,

at backend django change it to

HTTP_USER_AGENT : Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36

What's the need of this ? Any helpful suggestion will be appreciated .

Community
  • 1
  • 1
shuboy2014
  • 1,350
  • 2
  • 18
  • 44

2 Answers2

1

request.META is a dictionary containing django's constants as keys, not HTTP header names.

I am quoting:

With the exception of CONTENT_LENGTH and CONTENT_TYPE, as given above, any HTTP headers in the request are converted to META keys by converting all characters to uppercase, replacing any hyphens with underscores and adding an HTTP_ prefix to the name. So, for example, a header called X-Bender would be mapped to the META key HTTP_X_BENDER.

baldr
  • 2,891
  • 11
  • 43
  • 61
  • 1
    I have edited the question because its pycharm terminal bug . – shuboy2014 Nov 04 '16 at 15:33
  • 1
    It's worth noting that this is defined by the CGI spec (and by extension the WSGI spec), Django itself doesn't actually transform any of the headers. `request.META` is actually just the [WSGI `environ` variable](https://www.python.org/dev/peps/pep-3333/#environ-variables). – knbk Nov 04 '16 at 15:34
1

HTTP headers are case insensitive.

According to the Django docs, HTTP headers are converted to upper case, hyphens are converted to underscores, and the HTTP_ prefix is added. This means that you can use request.META['HTTP_USER_AGENT'] in your code, whether the request used User-Agent, USER-AGENT, or something else.

Community
  • 1
  • 1
Alasdair
  • 298,606
  • 55
  • 578
  • 516