Django + WSGI: no HTTP_COOKIE in environ

205 views
Skip to first unread message

need_some_help

unread,
Jul 10, 2018, 8:40:41 AM7/10/18
to Django users
I've followed numerous examples, but the only one that does not return errors are the implementations of the two functions below. There doesn't seem to be an HTTP_COOKIE key in the environ. The request value only gets set with POST and GET. Think something is wrong with my setup, because none of the common usage patterns I've seen online like request.POST don't work. According to the following:

https://stackoverflow.com/questions/1622793/django-cookies-how-can-i-set-them

https://www.tutorialspoint.com/django/django_cookies_handling.htm

https://overiq.com/django/1.10/cookies-in-django/

I should be using response.set_cookie() which is what I have:

view.py

def index(environ, start_response, request):
    print request
    print environ.keys()
    template = loader.get_template('index-test.html')
    response = HttpResponse(template.render(None))
    response.set_cookie('name1', 'test-cookie')
    response.set_cookie('name2', 'test-cookie')
    return response


wsgi.py:

def application(environ, start_response):
    try:
        request_body_size = int(environ.get('CONTENT_LENGTH', 0))
    except (ValueError):
        request_body_size = 0
    request_body = environ['wsgi.input'].read(request_body_size)
    request = parse_qs(request_body)

    start_response('200 OK', [('Content-Type', 'text/html')])
    # DO I NEED AT SET COOKIE HEADER HERE?
    return view.index(environ, start_response, request)

OUTPUT:

request: {}

environ.keys():
['RUN_MAIN', 'HTTP_REFERER', 'XDG_GREETER_DATA_DIR', 'QT4_IM_MODULE', 'SERVER_SOFTWARE', 'UPSTART_EVENTS', 'SCRIPT_NAME', 'XDG_SESSION_TYPE', 'REQUEST_METHOD', 'CONTENT_LENGTH', 'SERVER_PROTOCOL', 'HOME', 'DISPLAY', 'LANG', 'SHELL', 'PATH_INFO', 'XDG_DATA_DIRS', 'QT_LINUX_ACCESSIBILITY_ALWAYS_ON', 'MANDATORY_PATH', 'COMPIZ_CONFIG_PROFILE', 'UPSTART_INSTANCE', 'JOB', 'SESSION', 'LIBGL_ALWAYS_SOFTWARE', 'SERVER_PORT', 'CLUTTER_IM_MODULE', 'XMODIFIERS', 'GTK2_MODULES', 'HTTP_PRAGMA', 'XDG_RUNTIME_DIR', 'COMPIZ_BIN_PATH', 'VTE_VERSION', 'HTTP_CACHE_CONTROL', 'HTTP_CONNECTION', 'HTTP_HOST', 'wsgi.version', 'XDG_CURRENT_DESKTOP', 'XDG_SESSION_ID', 'DBUS_SESSION_BUS_ADDRESS', 'GNOME_KEYRING_PID', 'HTTP_ACCEPT', 'DESKTOP_SESSION', 'LESSCLOSE', 'DEFAULTS_PATH', 'wsgi.run_once', 'wsgi.errors', 'wsgi.multiprocess', 'HTTP_ACCEPT_LANGUAGE', 'INSTANCE', 'LS_COLORS', 'XDG_SEAT', 'GNOME_DESKTOP_SESSION_ID', 'LESSOPEN', 'QUERY_STRING', 'QT_IM_MODULE', 'LOGNAME', 'USER', 'GNOME_KEYRING_CONTROL', 'XDG_VTNR', 'PATH', 'TERM', 'HTTP_USER_AGENT', 'XDG_SESSION_PATH', 'XAUTHORITY', 'LANGUAGE', 'REMOTE_ADDR', 'SHLVL', 'QT_QPA_PLATFORMTHEME', 'wsgi.url_scheme', 'QT_ACCESSIBILITY', 'WINDOWID', 'SESSIONTYPE', 'IM_CONFIG_PHASE', 'GPG_AGENT_INFO', 'XDG_SESSION_DESKTOP', 'SSH_AUTH_SOCK', 'GDMSESSION', 'UPSTART_JOB', 'wsgi.multithread', 'XDG_SEAT_PATH', 'TZ', '_', 'wsgi.input', 'GTK_IM_MODULE', 'UPSTART_SESSION', 'XDG_CONFIG_DIRS', 'SERVER_NAME', 'GATEWAY_INTERFACE', 'OLDPWD', 'GDM_LANG', 'GTK_MODULES', 'PWD', 'DJANGO_SETTINGS_MODULE', 'CONTENT_TYPE', 'wsgi.file_wrapper', 'REMOTE_HOST', 'HTTP_ACCEPT_ENCODING']

You can see their is no HTTP_COOKIE in the keys. If I'm not setting it right, or there's something I need to check, please let me know.

Melvyn Sopacua

unread,
Jul 10, 2018, 2:53:37 PM7/10/18
to django...@googlegroups.com
On dinsdag 10 juli 2018 14:40:41 CEST need_some_help wrote:
> I've followed numerous examples, but the only one that does not return
> errors are the implementations of the two functions below. There doesn't
> seem to be an HTTP_COOKIE key in the environ. The request value only gets
> set with POST and GET. Think something is wrong with my setup, because none
> of the common usage patterns I've seen online like request.POST don't work.

...

> You can see their is no HTTP_COOKIE in the keys. If I'm not setting it
> right, or there's something I need to check, please let me know.

Do you know how cookies work? Specifically, that on the first request to a
server by a new browser (or command line), there never are cookies?

--
Melvyn Sopacua

need_some_help

unread,
Jul 19, 2018, 4:34:35 AM7/19/18
to Django users
Sorry for the delayed response. Yes, I understand that a cookie must be set/sent to the browser after the first request, and that generally you must send the cookie headers alone as I've had trouble on other platforms trying to send them along with other things. That being said, I need to cover the basics.

I first need to make sure I am accessing them correctly. This is as simple as it gets I imagine:


view.py:

def index(environ, start_response, request):
    if not 'HTTP_COOKIE' in environ:
        response = HttpResponse("hello")
        response.set_cookie('user_agreement', 'cookie text', domain='.mysite.com')
        return response
    else:
        print environ['HTTP_COOKIE']

The webpage just prints 'hello' and never reaches the else not matter how many times I refresh the page. There are no cookies in the browser ever either.

Am I missing some middleware? I remember some answers to cookie problems related to the order of their middleware configurations. Here's mine:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

Melvyn Sopacua

unread,
Jul 21, 2018, 3:19:44 AM7/21/18
to django...@googlegroups.com

On donderdag 19 juli 2018 10:34:35 CEST need_some_help wrote:

 

> I first need to make sure I am accessing them correctly. This is as simple

> as it gets I imagine:

>

> view.py:

>

> def index(environ, start_response, request):

> if not 'HTTP_COOKIE' in environ:

> response = HttpResponse("hello")

> response.set_cookie('user_agreement', 'cookie text',

> domain='.mysite.com')

> return response

> else:

> print environ['HTTP_COOKIE']

>

> The webpage just prints 'hello' and never reaches the else not matter how

> many times I refresh the page. There are no cookies in the browser ever

> either.

>

> Am I missing some middleware?

 

No, you have the wrong assumption about a view method's signature. The first argument is always the request, as in django.http.HttpRequest or more specifically, it's subclass django.core.handlers.WSGIRequest in the case of WSGI.

You're not writing a WSGI handler (who has environ as first argument). A view is two stops down and to give you the complete onion:

WSGI Handler -> Middleware -> view -> Middleware -> WSGI Handler

 

Anyway, the cookies are at request.COOKIES as per docs.

--

Melvyn Sopacua

Message has been deleted

need_some_help

unread,
Jul 21, 2018, 5:25:15 PM7/21/18
to Django users
Thank you Melvyn for your time and response. Regardless of what I call the first parameter, shouldn't HTTP_COOKIE be in it? But yes, I am having some confusion about wsgi and django and maybe I am mixing tutorials/information I have found at different sources.

I have had to reach out to other sources, perhaps the information I provided at the following link can help us figure out what I'm doing completely wrong:
https://groups.google.com/forum/#!topic/comp.lang.python/s47xpBs1714

Jason

unread,
Jul 21, 2018, 5:28:05 PM7/21/18
to Django users
Reply all
Reply to author
Forward
0 new messages