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.. ;)
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.
The default name for the cookie is PLAY_SESSION . This can be changed by configuring the key session. cookieName in application. conf.”
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")
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) {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With