How can I get all the request headers in Django?

前端 未结 9 2540
误落风尘
误落风尘 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:13

    If you want to get client key from request header, u can try following:

    from rest_framework.authentication import BaseAuthentication
    from rest_framework import exceptions
    from apps.authentication.models import CerebroAuth
    
    class CerebroAuthentication(BaseAuthentication):
    def authenticate(self, request):
        client_id = request.META.get('HTTP_AUTHORIZATION')
        if not client_id:
            raise exceptions.AuthenticationFailed('Client key not provided')
        client_id = client_id.split()
        if len(client_id) == 1 or len(client_id) > 2:
            msg = ('Invalid secrer key header. No credentials provided.')
            raise exceptions.AuthenticationFailed(msg)
        try:
            client = CerebroAuth.objects.get(client_id=client_id[1])
        except CerebroAuth.DoesNotExist:
            raise exceptions.AuthenticationFailed('No such client')
        return (client, None)
    

提交回复
热议问题