To get all headers, you can use request. headers. keys() or request. headers.
It's possible that a request can come in via POST with an empty POST dictionary -- if, say, a form is requested via the POST HTTP method but does not include form data. Therefore, you shouldn't use if request. POST to check for use of the POST method; instead, use if request. method == "POST" (see above).
GET requests can have "Accept" headers, which say which types of content the client understands. The server can then use that to decide which content type to send back. They're optional though.
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
andCONTENT_TYPE
, as given above, anyHTTP
headers in the request are converted toMETA
keys by converting all characters to uppercase, replacing any hyphens with underscores and adding anHTTP_
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_'))
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()
.
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:
-
with underscore _
HTTP_
to all headers in original request, except for CONTENT_TYPE
and CONTENT_LENGTH
.The values of each header should be unmodified.
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)
request.META.get('HTTP_AUTHORIZATION')
/python3.6/site-packages/rest_framework/authentication.py
you can get that from this file though...
I don't think there is any easy way to get only HTTP headers. You have to iterate through request.META dict to get what all you need.
django-debug-toolbar takes the same approach to show header information. Have a look at this file responsible for retrieving header information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With