84๐
Go ahead and use:
request.META.get('HTTP_{your uppercased header name}')
Note in Django you write the header name in capitals with underscores instead of dashes, but in the request on the client you must write it using dashes instead of underscores (production web servers will strip out custom headers with underscores in them for security reasons).
So, a custom header My-Custom-Header
is accessed request.META['HTTP_MY_CUSTOM_HEADER']
17๐
Finally I found just get it through
request.META('HTTP_{your uppercased header name}')
- [Django]-Stack trace from manage.py runserver not appearing
- [Django]-How to do SELECT MAX in Django?
- [Django]-Do CSRF attacks apply to API's?
16๐
You can add your own custom headers to a response like so:
https://docs.djangoproject.com/en/dev/ref/request-response/#setting-headers
>>> response = HttpResponse()
>>> response['Cache-Control'] = 'no-cache'
>>> del response['Cache-Control']
Or use a decorator to add them to a view:
http://djangosnippets.org/snippets/275/
- [Django]-Django set field value after a form is initialized
- [Django]-Make sure only one worker launches the apscheduler event in a pyramid web app running multiple workers
- [Django]-Python- How to flush the log? (django)
8๐
I was trying to access the header with the above answers, using this code:
request.META.get('HTTP_{your uppercased header name}')
But It didnโt work for me, and then I realized that the custom header should not contain underscore
so I changed the underscore
with dash
and boom, everything started working. Hope this will help people like me. ๐
- [Django]-Sending post data from angularjs to django as JSON and not as raw content
- [Django]-Django orm get latest for each group
- [Django]-Django csrf token + Angularjs
6๐
As of Django 2.2 you can use the HttpRequest.headers
dictionary which provides case-insensitive dictionary of request headers, like such:
my_header = request.headers.get('x-my-custom-header')
- [Django]-Sort order of Django Admin records
- [Django]-How do I pass template context information when using HttpResponseRedirect in Django?
- [Django]-*_set attributes on Django Models
3๐
From the Django documentation:
https://docs.djangoproject.com/en/2.1/ref/request-response/#django.http.HttpRequest.META
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.
- [Django]-AWS: can't connect to RDS database from my machine
- [Django]-PHP Frameworks (CodeIgniter, Yii, CakePHP) vs. Django
- [Django]-Django pass object to include