How can I get all the request headers in Django?

143,288

Solution 1

According to the documentation request.META is a "standard Python dictionary containing all available HTTP headers". If you want to get all the headers you can simply iterate through the dictionary.

Which part of your code to do this depends on your exact requirement. Anyplace that has access to request should do.

Update

I need to access it in a Middleware class but when i iterate over it, I get a lot of values apart from HTTP headers.

From the documentation:

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.

(Emphasis added)

To get the HTTP headers alone, just filter by keys prefixed with HTTP_.

Update 2

could you show me how I could build a dictionary of headers by filtering out all the keys from the request.META variable which begin with a HTTP_ and strip out the leading HTTP_ part.

Sure. Here is one way to do it.

import re
regex = re.compile('^HTTP_')
dict((regex.sub('', header), value) for (header, value) 
       in request.META.items() if header.startswith('HTTP_'))

Solution 2

Starting from Django 2.2, you can use request.headers to access the HTTP headers. From the documentation on HttpRequest.headers:

A case insensitive, dict-like object that provides access to all HTTP-prefixed headers (plus Content-Length and Content-Type) from the request.

The name of each header is stylized with title-casing (e.g. User-Agent) when it’s displayed. You can access headers case-insensitively:

>>> request.headers
{'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6', ...}

>>> 'User-Agent' in request.headers
True
>>> 'user-agent' in request.headers
True

>>> request.headers['User-Agent']
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)
>>> request.headers['user-agent']
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)

>>> request.headers.get('User-Agent')
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)
>>> request.headers.get('user-agent')
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)

To get all headers, you can use request.headers.keys() or request.headers.items().

Solution 3

This is another way to do it, very similar to Manoj Govindan's answer above:

import re
regex_http_          = re.compile(r'^HTTP_.+$')
regex_content_type   = re.compile(r'^CONTENT_TYPE$')
regex_content_length = re.compile(r'^CONTENT_LENGTH$')

request_headers = {}
for header in request.META:
    if regex_http_.match(header) or regex_content_type.match(header) or regex_content_length.match(header):
        request_headers[header] = request.META[header]

That will also grab the CONTENT_TYPE and CONTENT_LENGTH request headers, along with the HTTP_ ones. request_headers['some_key] == request.META['some_key'].

Modify accordingly if you need to include/omit certain headers. Django lists a bunch, but not all, of them here: https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.META

Django's algorithm for request headers:

  1. Replace hyphen - with underscore _
  2. Convert to UPPERCASE.
  3. Prepend HTTP_ to all headers in original request, except for CONTENT_TYPE and CONTENT_LENGTH.

The values of each header should be unmodified.

Solution 4

Simply you can use HttpRequest.headers from Django 2.2 onward. Following example is directly taken from the official Django Documentation under Request and response objects section.

>>> request.headers
{'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6', ...}

>>> 'User-Agent' in request.headers
True
>>> 'user-agent' in request.headers
True

>>> request.headers['User-Agent']
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)
>>> request.headers['user-agent']
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)

>>> request.headers.get('User-Agent')
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)
>>> request.headers.get('user-agent')
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)

Solution 5

request.META.get('HTTP_AUTHORIZATION') /python3.6/site-packages/rest_framework/authentication.py

you can get that from this file though...

Share:
143,288
Mridang Agarwalla
Author by

Mridang Agarwalla

I'm a software developer who relishes authoring Java and Python, hacking on Android and toying with AppEngine. I have a penchant for development and a passion for the business side of software. In between all the work, I contribute to a number of open-source projects, learn to master the art of cooking Asian cuisine and try to stay sane while learning to fly my Align Trex-600 Nitro Heli.

Updated on December 16, 2021

Comments

  • Mridang Agarwalla
    Mridang Agarwalla over 2 years

    I need to get all the Django request headers. From what I've read, Django simply dumps everything into the request.META variable along with a lot of other data. What would be the best way to get all the headers that the client sent to my Django application?

    I'm going use these to build a httplib request.

  • Mridang Agarwalla
    Mridang Agarwalla over 13 years
    I need to access it in a Middleware class but when i iterate over it, I get a lot of values apart from HTTP headers.
  • Mridang Agarwalla
    Mridang Agarwalla over 13 years
    Thanks Manoj. Just out of curiosity - could you show me how I could build a dictionary of headers by filtering out all the keys from the request.META variable which begin with a HTTP_ and strip out the leading HTTP_ part. Is this possible through lambda functions? (I think they're called lambda functions) I'm asking this because I would probably go on do it the long way by first iterating over them, then checking to see if it begins with a HTTP_ and then adding it to the new dictionary. Thanks again.
  • Mridang Agarwalla
    Mridang Agarwalla over 13 years
    Thanks again Manoj. I modified it slightly to use lstrip('HTTP_') instead of the regex. :)
  • Manoj Govindan
    Manoj Govindan over 13 years
    @Mridang: lstrip. Of course. My substitute fu is weak today evening.
  • Admin
    Admin over 12 years
    This does not seem to work with agents like CURL. It appears CURL is not setting the META dictionary while typical browsers do.
  • jcdyer
    jcdyer about 10 years
    @Mridang Agarwalla: lstrip won't actually do what you're asking it to do. lstrip will strip out all leading characters that match any characters in the string you give it, so if you have a header "HTTP_TOKEN_ID" it will give back "OKEN_ID", because the "T" at the beginning of "TOKEN" matches a character in the string passed to lstrip. The way to do it is prefix = 'HTTP_'; header = header[len(prefix):].
  • freb
    freb over 9 years
    I'll be using the headers for a new request, treating Django as a proxy and I was worried about the headers being all upper-case. Turns out HTTP headers are case-insensitive, so this isn't an issue.
  • Rebs
    Rebs over 8 years
    That can all be combined into a single regexp, re.compile(r'^(HTTP_.+|CONTENT_TYPE|CONTENT_LENGTH)$')
  • 周左左
    周左左 about 5 years
    Django 2.2 has supported HttpRequest.headers.