Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read the value of a cookie in the Play-Framework with Scala?

How can I read the value of a cookie in my controller in the Play-Framework with Scala?

In my controller I have this:

println(Http.Request.current().headers.get("cookie"))

And the output is this:

[csrftoken=011d7cfe84915ee9897c8c6079d49d5a; test=value]

And I'm hoping there is a better way of accessing the value of "test" other than parsing the string.. ;)

like image 413
Jay Taylor Avatar asked Jun 06 '11 08:06

Jay Taylor


People also ask

What is play framework in Scala?

Play Framework makes it easy to build web applications with Java & Scala. Play is based on a lightweight, stateless, web-friendly architecture. Built on Akka, Play provides predictable and minimal resource consumption (CPU, memory, threads) for highly-scalable applications.

What is the default session cookie name set by the Play framework?

The default name for the cookie is PLAY_SESSION . This can be changed by configuring the key session. cookieName in application. conf.”


2 Answers

You can access the cookie using the cookie object on the HTTP Request, rather than getting it in raw format from the header. Look at the API here for more info.

You should be able to just do:

Http.Request.current().cookies.get("test")

like image 198
Codemwnci Avatar answered Nov 10 '22 07:11

Codemwnci


You can get the cookie value in scala template using @request.cookies.get("email").value.

If you want to check its not null, @if(request.cookies.get("email") != null) {}

like image 32
Sivailango Avatar answered Nov 10 '22 06:11

Sivailango