0

I'm having some issues retrieving cookies sent by the client on the server using the python module cherrypy.

In the web browser I can see the cookies in Chrome's Web Console by going to Resources -> Cookies -> localhost. And then I have 3 cookies with the following values.

Chrome web cookies

And on the server side I have the following information from the Eclipse debugger:

cp2

My question is why don't foo and release_id show up in the keys? Is there something I'm doing wrong?

NOTE:

cookies = cherrypy.request.cookie

Javascript code for setting the cookies:

$(function() {

// Functions from W3C Schools: http://www.w3schools.com/js/js_cookies.asp
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString() + ";";
    var path = "path=/;" // Accessible to all.
    document.cookie = cname + "=" + cvalue + "; " + expires + path;
}

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
    }
    return "";
}

setCookie("foo", "bar", 1 );
setCookie("release_id", "2", 1 );
});
Lucas
  • 2,514
  • 4
  • 27
  • 37
  • Please show the code for populating foo and release_id. – Andrew Kloos Oct 07 '14 at 19:28
  • @AndrewKloos I've added the Javascript code. – Lucas Oct 07 '14 at 19:47
  • Oh I see... the cherrypy isn't really involved at this point. Check out this post http://stackoverflow.com/questions/14573223/set-cookie-and-get-cookie-with-javascript – Andrew Kloos Oct 07 '14 at 19:58
  • My question is why would the cookies show up in the cherrypy.request.cookie dictionary, but then cherrypy.request.cookie['foo'] fails with a KeyError? – Lucas Oct 07 '14 at 20:11
  • ok, well try to redirect after the js sets the cookie and see if cherrypy has the key pair for the next request. Also, are you sure your session is sticking around and not being recreated each request? – Andrew Kloos Oct 07 '14 at 20:15
  • I made a dumb mistake in my python code, mistaking response for request. Thank you for your help. – Lucas Oct 07 '14 at 20:59

1 Answers1

0

Are you sure that the cookies.keys is not cherrypy.response.cookie?

cherrypy.response.cookie != cherrypy.request.cookie

From the debugger print-screen perspective, seems ok to havecherrypy.response.cookie with just thesession_id key at this point.

cyraxjoe
  • 5,661
  • 3
  • 28
  • 42