[Django]-How to check contents of incoming HTTP header request

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.

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
๐Ÿ‘คdting

2๐Ÿ‘

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"]
๐Ÿ‘คWilliam Niu

Leave a comment