Question text/background and finds
My browser sends some HTTP header (e.g. referer) to my application I’m building in the play 2.0 framework. I’m completly clueless how to read them so i can pass them on (google isn’t helping).
I think I might need to do something mentioned here (http://www.playframework.org/documentation/2.0/ScalaInterceptors). Which resulted in:
override def onRouteRequest(request: RequestHeader): Option[Handler] = {
println("headers:" + request.headers.toString)
super.onRouteRequest(request)
}
Which works outputting to the console. But I don’t know how to pass them to a specific controller action. I could, for example, add an if statement and call myAction once it sees a specific ‘route’ (eg /client/view/123) or call super.onRouteRequest(request) otherwise. But I’d lose the functionality in my /conf/routing. What is the ‘proper’ way of doing this?
In my queste to answer that I found this: Http.Context.current().request() but using that in my controller action gave me a [RuntimeException: There is no HTTP Context available from here.].
Another thing I found is this where Guillaume Bort replies to a, I think unrelated question:
I'm not sure what you are trying to do but:
case class CustomRequest(token: String, request: Request[AnyContent])
extends WrappedRequest(request)
case class CustomAction(f: CustomRequest => Result)
extends Action[AnyContent] {
lazy val parser = BodyParsers.parse.anyContent
def apply(req: Request[AnyContent]) = req match {
case r: CustomRequest => f(r)
case _ => sys.error("Invalid usage")
}
}
object Application extends Controller {
def index = CustomAction { request =>
Ok("Token: " + request.token)
}
}
With onRouteRequest:
override def onRouteRequest(req: RequestHeader) = {
super.onRouteRequest(req).map { _ match {
case a: CustomAction => Action { (request: Request[AnyContent]) =>
a(CustomRequest("XXX", request))
}
case o => o
}
}
}
But that is a bit over my head atm (and perhaps not even an answer to my question). But if this is the way to go let me know.
Question summarized
What would be the proper/nice way to read HTTP Headers sent by the browser in a contoller action? I only care/need the HTTP Header in a few routes.
Thanks for any pointers or nudges!
PS:
1) I’m new to scala and play (and development on the web via rails like frameworks), so my apology for any lingo errors (do tell).
2) New to stackoverflow as well … but it looks awsome, hope I did everything OK for my first question here!
3) I had 5 links/finds but no reputation to allow that, so narrowed my question down to the 3 interesting webfinds , sorry.
You can read headers using the examples here.
Essentially:
You can change the headers in a response using this example.