Alexander
Alexander

Reputation: 3

How to read cookies in Scala play?

I'm adding Cookie like so:

              Redirect(routes.UserPage.form(usersignin.email)).withCookies(Cookie("guid", md5hash1cookie))

How could i read it?

If i use:

println(Http.Request.current().cookies.get("guid"));

and i get error:

not found: value Http

UPD 1:

Correct way to use is 
  def form(msg: String = "") = Action {
    request => {
      //  guid = guId.toString();
        println(request.cookies.get("guid"));
...

  }
}

How to get value out of coockie? println(request.cookies.get("guid")); returns Some(Cookie(guid,7a3bdea2ba59a196c02fb7bdbcdb4e26,None,/,None,false,false))

and i need just 7a3bdea2ba59a196c02fb7bdbcdb4e26 returned as string.

Solution:

for(gu <- request.cookies.get("guid")){
          println(gu.value);
      }

Upvotes: 0

Views: 733

Answers (1)

johanandren
johanandren

Reputation: 11479

You don't have a shared state containing the requests in the Scala Play APIs, instead you will have to use the Action { request => way to define actions, on the request you can then access the cookies through request.cookies

Upvotes: 1

Related Questions