Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to read the browser HTTP Header sent to play in a controller action?
    primarykey
    data
    text
    <h2>Question text/background and finds</h2> <p>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).</p> <p>I <em>think</em> I might need to do something mentioned here (http://www.playframework.org/documentation/2.0/ScalaInterceptors). Which resulted in:</p> <pre><code>override def onRouteRequest(request: RequestHeader): Option[Handler] = { println("headers:" + request.headers.toString) super.onRouteRequest(request) } </code></pre> <p>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 <code>if</code> statement and call <code>myAction</code> once it sees a specific 'route' (eg /client/view/123) or call <code>super.onRouteRequest(request)</code> otherwise. But I'd lose the functionality in my <code>/conf/routing</code>. What is the 'proper' way of doing this?</p> <p>In my queste to answer that I found <a href="https://groups.google.com/d/msg/play-framework/11zbMtNI3A8/q5IuEBcIefcJ" rel="nofollow">this</a>: <code>Http.Context.current().​request()</code> but using that in my controller action gave me a <code>[RuntimeException: There is no HTTP Context available from here.]</code>.</p> <p>Another thing I found is <a href="https://groups.google.com/d/msg/play-framework/ZBLeUTo9aMk/XTzPDQZHcc8J" rel="nofollow">this</a> where Guillaume Bort replies to a, I <em>think</em> unrelated question:</p> <pre><code>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 =&gt; Result) extends Action[AnyContent] { lazy val parser = BodyParsers.parse.anyContent def apply(req: Request[AnyContent]) = req match { case r: CustomRequest =&gt; f(r) case _ =&gt; sys.error("Invalid usage") } } object Application extends Controller { def index = CustomAction { request =&gt; Ok("Token: " + request.token) } } With onRouteRequest: override def onRouteRequest(req: RequestHeader) = { super.onRouteRequest(req).map { _ match { case a: CustomAction =&gt; Action { (request: Request[AnyContent]) =&gt; a(CustomRequest("XXX", request)) } case o =&gt; o } } } </code></pre> <p>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.</p> <h2>Question summarized</h2> <p>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.</p> <p>Thanks for any pointers or nudges!</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload