Play Framework examine HTTP request
15,562
Solution 1
Within an action, you can get the request header using the request()
method, for instance, in Java:
public static Result index() {
// example of a Header
String userAgent = request().getHeader("User-Agent");
// example of a query parameter
String q = request().getQueryString("q");
...
}
You can take a look at the API for Java or Scala.
Solution 2
Better use a constant than a hardcoded String, in Scala the code is
import play.mvc.Http
val userAgent: String = request.headers.get(Http.HeaderNames.USER_AGENT).get
Solution 3
This line worked for me:
implicit request => val User-Agent:String = request.headers.get("User-Agent").get
Author by
ticofab
Updated on June 20, 2022Comments
-
ticofab almost 2 years
I was wondering how to examine an HTTP request in Play Framework 2.1. The only information I can find on the documentation is via the
conf/routes
mechanism:GET /clients/:id controllers.Clients.show(id: Long)
but this will only allow us to get the parameter
id
from the path. How do I access other part of the request, such as header or query params? In other words, what are Play's equivalents of JAX-RS@HeaderParam
,@FormParam
,@QueryParam
and such? -
ticofab about 11 yearsThank you nico_ekito! I'd be also very happy if you could you point me to some more extensive guide: the one on the website seems to only scratch the surface.
-
ndeverge about 11 yearsI do not know any particular guide, instead of the official doc. There are also some books out there bit.ly/playscala and bit.ly/playjava
-
supertonsky about 10 yearsCan you please at least provide where request() came from? For novices' sake please.
-
ndeverge about 10 yearsIt is inherited from the Controller class