如何检查传入的HTTP标头请求的内容 [英] How to check contents of incoming HTTP header request

查看:180
本文介绍了如何检查传入的HTTP标头请求的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm playing around with some APIs and I'm trying to figure this out.

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

I am making a basic HTTP authenticated request to my server via the API. As part of this request, the authenticated key is stored in the HTTP header as username.

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

So my question is, how do I get the contents of the incoming request such that I can perform a check against it?

我想做的事情:

if incoming request has header == 'myheader':
    do some stuff
else:
    return ('not authorised')

对于有兴趣的人,我正试图上班。

For those interested, I am trying to get this to work.

UPDATE
我正在使用Django

UPDATE I am using 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。

With the exception of CONTENT_LENGTH and CONTENT_TYPE, as given above, any 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, for example, a header called X-Bender would be mapped to the META key HTTP_X_BENDER.

所以:

if request.META['HTTP_USERNAME']:
    blah
else:
    blah

这篇关于如何检查传入的HTTP标头请求的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆