MhmdRizk
MhmdRizk

Reputation: 1721

Passing a header value to a get request in django

I want to pass a value through the Headers of a get request.

Im trying the below but it doesn't work,

class ListCategoriesView(generics.ListAPIView):
"""
Provides a get method handler.
"""
serializer_class = CategorySerializer

def get(self, request, *args, **kwargs):
    token = request.data.get("token", "")
    if not token:
      """ 
          do some action here
      """
    if not UserAccess.objects.filter(accessToken=token).exists():
      """ 
          do some action here
      """
    else:
      """ 
          do some action here
      """

I want to pass the token in the headers like that :

enter image description here

can anyone help me with this issue, thanks a lot in advance.

Upvotes: 3

Views: 2757

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 600041

You said it yourself, you're passing it in the headers, so you need to get it from there. DRF does not do anything special to access the headers, so it proxies to the underlying Django HttpRequest object, which makes them available via the META attribute, converted to uppercase and prefixed by HTTP_:

token = request.META.get("HTTP_TOKEN", "")

Upvotes: 5

Related Questions