Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7690427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:23:39+00:00 2026-05-31T20:23:39+00:00

Question text/background and finds My browser sends some HTTP header (e.g. referer) to my

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:23:40+00:00Added an answer on May 31, 2026 at 8:23 pm

    You can read headers using the 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 a response using this example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some text before the code so that the question summary isn't mangled. class Tree
Some background information, you can skip this part for the actual question this is
Background: It seems that some text editors and IDEs are starting to get more
Background: JEdit (and some other text editors as well) support a feature called Multiple
Another clipboard question: When text is put onto the clipboard, it frequently goes in
Following on from my previous question [link text][1] , I have a new problem.
In the context of this question link text is possible from a Controller that
I searched for this and found Maudite's question about text editors but they were
I want to implement this view But I have one question: The text which
This is a piece of code I write for this question: Entry text on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.