简体   繁体   English

在Django中将标头值传递给get请求

[英]Passing a header value to a get request in django

I want to pass a value through the Headers of a get request. 我想通过get请求的标头传递一个值。

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 : 我想像这样在标头中传递令牌:

在此处输入图片说明

can anyone help me with this issue, thanks a lot in advance. 任何人都可以帮助我解决这个问题,谢谢。

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_ : DRF对访问标头没有做任何特殊的事情,因此它代理底层的Django HttpRequest对象,这使它们可以通过META属性使用 ,转换为大写并以HTTP_作为前缀:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM