开发者

How to check contents of incoming HTTP header request

开发者 https://www.devze.com 2023-02-19 14:54 出处:网络
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 stor

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开发者_如何学Go 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


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


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"]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号