[英]How to check contents of incoming HTTP header request
我正在玩一些API,我试图解决这个问题。
我正在通过API向我的服务器发出基本的HTTP身份验证请求。 作为此请求的一部分,经过身份验证的密钥将作为用户名存储在HTTP标头中。
所以我的问题是,如何获取传入请求的内容,以便我可以对其进行检查?
我想做什么:
if incoming request has header == 'myheader':
do some stuff
else:
return ('not authorised')
对于那些感兴趣的人,我试图让这个工作。
更新我正在使用Django
http://docs.djangoproject.com/en/dev/ref/request-response/
HttpRequest.META
A standard Python dictionary containing all available HTTP headers.
Available headers depend on the client and server, but here are some examples:
CONTENT_LENGTH
CONTENT_TYPE
HTTP_ACCEPT_ENCODING
HTTP_ACCEPT_LANGUAGE
HTTP_HOST -- The HTTP Host header sent by the client.
HTTP_REFERER -- The referring page, if any.
HTTP_USER_AGENT -- The client's user-agent string.
QUERY_STRING -- The query string, as a single (unparsed) string.
REMOTE_ADDR -- The IP address of the client.
REMOTE_HOST -- The hostname of the client.
REMOTE_USER -- The user authenticated by the Web server, if any.
REQUEST_METHOD -- A string such as "GET" or "POST".
SERVER_NAME -- The hostname of the server.
SERVER_PORT -- The port of the server.
除了上面给出的CONTENT_LENGTH和CONTENT_TYPE之外,请求中的任何HTTP头都将转换为META密钥,方法是将所有字符转换为大写,用下划线替换任何连字符,并在名称中添加HTTP_前缀。 因此,例如,名为X-Bender的标头将映射到META密钥HTTP_X_BENDER。
所以:
if request.META['HTTP_USERNAME']:
blah
else:
blah
标头存储在os.environ
。 因此,您可以像这样访问HTTP标头:
import os
if os.environ.haskey("SOME_HEADER"):
# do something with the header, i.e. os.environ["SOME_HEADER"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.