Search code examples
playframeworkplayframework-2.4cookie-session

Play with Play cookie session


When I set a value to session (cookie session), I can see that value from the client like below

firefox request cookies

However, when I try to get the cookie with javascript document.cookie, this is all I get

enter image description here

How can I get the PLAY_SESSION cookie?


Solution

  • You can't get the session cookie in the JavaScript of your browser. For security reasons it's set to http-only.

    If you just want to persist some small data between requests you can set your own cookie in the response of an action. Just set the http-only flag to false:

    response().setCookie(
        "theme",        // name
        "blue",         // value
        3600,           // maximum age
        "/some/path",   // path
        ".example.com", // domain
        false,          // secure
        false           // http only
    );
    

    With the http-only flag set to false you can access your cookie from your JavaScript within the browser.