[英]Request Headers within Python Django
What is the best method to extract a HTTP header from the request within my Django view. 从我的Django视图中的请求中提取HTTP标头的最佳方法是什么。
The header I need is X-Auth-Token
. 我需要的标头是X-Auth-Token
。
Would the following be the recommended method ? 推荐以下方法吗?
if request.META['HTTP_XAUTHTOKEN']:
print "header found"
else:
print "header not found"
request.META
is a dict, so if that header is not found, your code will fail with a KeyError
. request.META
是一个字典,因此,如果找不到该标头,则您的代码将失败,并显示KeyError
。 You can either catch that, or use request.META.get('HTTP_X_AUTHTOKEN')
instead. 您可以捕获该消息,也可以使用request.META.get('HTTP_X_AUTHTOKEN')
代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.