How can I get all the request headers in Django?

前端 未结 9 2543
误落风尘
误落风尘 2020-11-29 20:57

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 aof other dat

相关标签:
9条回答
  • 2020-11-29 21:19

    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.

    0 讨论(0)
  • 2020-11-29 21:28

    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().

    0 讨论(0)
  • 2020-11-29 21:30

    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)
    
    0 讨论(0)
提交回复
热议问题