Reputation: 550
Is it possible to add a cookie in the Play Framework's doFilter method in the Global class?
I've tried:
override def doFilter(action: EssentialAction): EssentialAction = EssentialAction { request =>
if (request.queryString.contains("q")) {
action.apply(request).map(_.withCookies(
Cookie("q", request.queryString.get("q").get(0), 3600)
))
}
}
but the cookie doesn't get sent to the browser.
Upvotes: 2
Views: 807
Reputation: 23851
I'm using Playframework 2.2 and use a similar thing:
import play.api.mvc._
import play.api._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
object ResponseFilter extends Filter {
def apply(next: (RequestHeader) => Future[SimpleResult])(rh: RequestHeader) = {
next(rh).map(_.withHeaders("Access-Control-Allow-Origin" -> "*").as("application/json; charset=utf-8"))
}
}
And my Global
object looks like this:
import play.api._
import play.api.mvc._
import service.ResponseFilter
object Global extends WithFilters(ResponseFilter) with GlobalSettings
This works fine for me. So I suppose you could replace the _.withHeaders(...)
part with the _.withCookies(...)
part and that would work for you as well.
Please note that things are a bit different in earlier versions of playframework
Upvotes: 1