[Django]-Retrieve HTTP Header in Django RestFrameWork

12👍

You need to lookup HTTP_AUTHORIZATION key instead of AUTHORIZATION because Django appends HTTP_ prefix to the header name.

From the Django docs on request.META:

With the exception of CONTENT_LENGTH and CONTENT_TYPE, any HTTP
headers in the request are converted to META keys by
converting all characters to uppercase, replacing any hyphens with
underscores and adding an HTTP_ prefix to the name. So, for example, a
header called X-Bender would be mapped to the META key HTTP_X_BENDER.

So, to retrieve the API key, you need to do:

API_key = request.META.get('HTTP_AUTHORIZATION')

Leave a comment