Reputation: 3466
I'm trying to post json data through a view to be processed and put in a view. The json contains a custom header named x-pinpoint-token. However, when I try and get this data through request.META.get it cannot get the data I need.
class Data(View):
@method_decorator(csrf_exempt)
def dispatch(self, *args, **kwargs):
return super(IDFAData, self).dispatch(*args, **kwargs)
def post(self, request, *args, **kwargs):
token = request.META.get('X_PINPOINT_TOKEN')
if token is None:
return HttpResponse(
"Failed Authorization - 401",
status='401'
)
else:
token = token.split(':')
token_clean = token[1]
data_in = json.loads(request.body)
...
No matter what I pass through the view token seems to be empty.
A test I ran with python-requests
:
import requests
token = '8756990800504b3f86a103bba1a03aab'
token = 'Token:'+token
data_in = {...}
import json
headers = {}
headers['content-type'] = 'application/json'
headers['X_PINPOINT_TOKEN'] = token
payload = json.dumps(data_in)
r = requests.post('http://0.0.0.0:5000/api/', headers=headers, data=payload)
but it just returns 401.
Upvotes: 1
Views: 1581
Reputation: 48952
The documentation for HttpRequest.META
says:
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 try request.META.get('HTTP_X_PINPOINT_TOKEN')
.
(An easy way to debug this would be to print or log request.META.keys()
.)
Upvotes: 6