开发者

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 acce开发者_Python百科ssing the value of "test" other than parsing the string.. ;)


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) {}


I am using Play 2.6.19 with Scala and I fetch as below;

val leadToken = request.cookies.get("lead_token") match {
 case Some(cookie) => cookie.value
 case None => ""
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜