How can I get all the request headers in Django

0 votes
I need to get all the Django request headers. As 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.
Jun 30, 2020 in Python by kartik
• 37,510 points

edited Jun 30, 2020 by kartik 12,245 views

1 answer to this question.

0 votes

Hello @kartik,

 You can use request.headers to access the HTTP 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().

Hope it helps!!

Thank You!!

answered Jun 30, 2020 by Niroj
• 82,880 points

Related Questions In Python

0 votes
2 answers

How can I get the count of a list in Python?

n=[1,2,3,4,5,6,7,8,9] print(len(n)) =9 READ MORE

answered Dec 10, 2020 in Python by anonymous
1,183 views
0 votes
1 answer

how do i get the list of all the keys in my dictionary?

my_dict = {'one': 'first', 'two': 'second', 'three': ...READ MORE

answered Sep 30, 2020 in Python by VJ_python
536 views
0 votes
0 answers

How can i get the content in the JSON format while making the GET requests in python?

While making POST requests how can we ...READ MORE

Jun 6, 2019 in Python by Waseem
• 4,540 points
441 views
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,880 points
2,097 views
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,880 points
1,246 views
0 votes
1 answer

How can I get the username of the logged-in user in Django?

Hello @kartik, You can use the request object ...READ MORE

answered Jul 29, 2020 in Python by Niroj
• 82,880 points
10,195 views
0 votes
1 answer

How can I get the absolute URL (with domain) in Django?

Hello @kartik, Use handy request.build_absolute_uri() method on request, pass it ...READ MORE

answered Aug 6, 2020 in Python by Niroj
• 82,880 points
9,778 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP