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

发布于 2024-10-25 06:41:16 字数 459 浏览 3 评论 0原文

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

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

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

我想做的事情:

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

对于那些感兴趣的人,我正在尝试获取 this去工作。

更新 我正在使用 Django

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

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?

What I am trying to do:

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
I am using Django

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听风吹 2024-11-01 06:41:16

http://docs.djangoproject.com/en/dev/ref/request-响应/

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

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.

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.

So:

if request.META['HTTP_USERNAME']:
    blah
else:
    blah
世俗缘 2024-11-01 06:41:16

标头存储在 os.environ 中。因此,您可以像这样访问 HTTP 标头:

import os
if os.environ.haskey("SOME_HEADER"):
  # do something with the header, i.e. os.environ["SOME_HEADER"]

The headers are stored in os.environ. So you can access the HTTP headers like this:

import os
if os.environ.haskey("SOME_HEADER"):
  # do something with the header, i.e. os.environ["SOME_HEADER"]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文