如何检查传入的HTTP标头请求的内容

sup*_*er9 3 python django basic-authentication http-headers

我正在玩一些API,我试图解决这个问题.

我正在通过API向我的服务器发出基本的HTTP身份验证请求.作为此请求的一部分,经过身份验证的密钥将作为用户名存储在HTTP标头中.

所以我的问题是,如何获取传入请求的内容,以便我可以对其进行检查?

我想做什么:

if incoming request has header == 'myheader':
    do some stuff
else:
    return ('not authorised')
Run Code Online (Sandbox Code Playgroud)

对于那些感兴趣的人,我试图让这个工作.

更新 我正在使用Django

DTi*_*ing 6

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.
Run Code Online (Sandbox Code Playgroud)

除了上面给出的CONTENT_LENGTH和CONTENT_TYPE之外,请求中的任何HTTP头都将转换为META密钥,方法是将所有字符转换为大写,用下划线替换任何连字符,并在名称中添加HTTP_前缀.因此,例如,名为X-Bender的标头将映射到META密钥HTTP_X_BENDER.

所以:

if request.META['HTTP_USERNAME']:
    blah
else:
    blah
Run Code Online (Sandbox Code Playgroud)