[英]having trouble with custom http headers in django
I'm trying to post json data through a view to be processed and put in a view. 我正在尝试通过一个视图发布json数据,以进行处理并放入一个视图中。 The json contains a custom header named x-pinpoint-token.
json包含一个名为x-pinpoint-token的自定义标头。 However, when I try and get this data through request.META.get it cannot get the data I need.
但是,当我尝试通过request.META.get获取此数据时,无法获取我需要的数据。
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
: 我使用
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. 但它只会返回401。
The documentation for HttpRequest.META
says: HttpRequest.META
的文档说:
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.
通过将所有字符都转换为大写字母,用下划线替换所有连字符并在名称中添加HTTP_前缀,将请求中的HTTP标头转换为META密钥。
So try request.META.get('HTTP_X_PINPOINT_TOKEN')
. 因此,请尝试
request.META.get('HTTP_X_PINPOINT_TOKEN')
。
(An easy way to debug this would be to print or log request.META.keys()
.) (一种简单的调试方法是打印或记录
request.META.keys()
。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.