I'm using django rest framework to implement a small function, which is to provide an API for our partners to access some data. The backend has already been written and I'm only writing the API to tap into it to get some data, so I'm just using function-based views to simplify things. Here's my test code:
@api_view(['GET'])
@authentication_classes((BasicAuthentication,))
@permission_classes((IsAuthenticated,))
def get_key(request):
username = request.user.username
enc = encode(key, username)
return Response({'API_key': enc, 'username': username}, status=status.HTTP_200_OK)
@api_view(['GET'])
def get_data(request):
user = request.user
API_key = request.META.get('Authorization') # the value is null
return Response({'API_key': API_key})
So the logged-in user first get an API key by calling get_key(request). Then he uses the API key to get the data. The problem is that I cannot retrieve the key that is put into the Authorization header:
headers = {'Authorization': 'yNd5vdL4f6d4f6dfsdF29DPh9vUtg=='}
r = requests.get('http://localhost:8000/api/getdata', headers=headers)
So I'm wondering how to get the header fields in django rest framework?