Django receiving custom request headers (headers) detailed

created at 07-17-2021 views: 16


Using Django as the backend, the client requests data from Django. In order to distinguish different requests, if you want to add each request category to the HTTP headers, you need to extract the corresponding parameters from the request header. The extraction method is as follows:

How to obtain:

The reuqest passed through the view function, use request.META.get("header key") to get.


from django.views import View
from django.http import HttpResponse

class ApiView(View):
    def get(self, request):
        ua = request.META.get('HTTP_USER_AGENT', 'unknown')
        return HttpResponse("Your browser is %s" % ua)


  1. The header key must be capitalized and the prefix must be "HTTP". For example, if your key is username, then it should be written as: request.META.get("HTTP_USERNAME")
  2. If the connector is a horizontal line - later, it will be automatically converted to an underscore _. For example, if the key of your header is api-auth, you should use request.META.get("HTTP_API_AUTH") to get the data of the request header in Django.
created at:07-17-2021
edited at: 07-17-2021: