How do I read the browser HTTP Header sent to play in a controller action?

Question text / background and search

My browser is sending some HTTP header (like a referent) to my application that I am building in the play 2.0 platform. I have absolutely no idea how to read them, so I can transfer them (Google doesn't help).

I think I might need to do something mentioned here (http://www.playframework.org/documentation/2.0/ScalaInterceptors). As a result:

override def onRouteRequest(request: RequestHeader): Option[Handler] = {
   println("headers:" + request.headers.toString)
   super.onRouteRequest(request)
}

      

What works with the console. But I don't know how to pass them to a specific controller action. For example, I could add a statement if

and call myAction

after it sees a specific "route" (eg / client / view / 123) or calls super.onRouteRequest(request)

otherwise. But I would lose functionality in mine /conf/routing

. What is the “correct” way to do this?

On my quest to answer, I found this : Http.Context.current().​request()

but using that in my controller action gave me [RuntimeException: There is no HTTP Context available from here.]

.

Another thing I found is this , where Guillaume Bort answers the question, 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's a bit over my head (and maybe not even an answer to my question). But if this is the way to go, let me know.

Generalized question

What would be the correct / nice way to read the HTTP headers sent by the browser in the contoller action? I only need / need the HTTP header on multiple routes.

Thanks for any pointers or hints!

PS: 1) I am new to scala and am playing (and developing on the web via rails like frameworks) so I apologize for any mistakes in lingua (will say). 2) New to stackoverflow, but ... but it looks awful, hope I did everything OK for my first question here! 3) I had 5 links / finds but no rep, so I narrowed my question down to 3 interesting web pages, sorry.

+3


source to share


1 answer


You can read the headings using examples here .

Essentially:



package controllers

import play.api.mvc._

object Application extends Controller {

  def index = Action {
    request =>
    // your code that reads request.headers here
    Ok("Got request [" + request + "]")
  }

}

      

You can change the headers in the response using this example .

+3


source







All Articles