Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Framework examine HTTP request

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?

like image 486
ticofab Avatar asked Feb 14 '13 11:02

ticofab


3 Answers

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.

like image 74
ndeverge Avatar answered Sep 22 '22 13:09

ndeverge


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
like image 21
Raymond Chenon Avatar answered Sep 22 '22 13:09

Raymond Chenon


This line worked for me:

implicit request => val User-Agent:String = request.headers.get("User-Agent").get
like image 31
Manasa Venkatakrishnan Avatar answered Sep 22 '22 13:09

Manasa Venkatakrishnan