Play Framework validates the HTTP request

I was wondering how to validate an HTTP request in Play Framework 2.1. The only information I can find in the documentation is the mechanism conf/routes

:

GET   /clients/:id          controllers.Clients.show(id: Long)  

      

but this will allow us to get the parameter id

from the path. How can I access another part of the request, such as the header or request parameters? In other words, what are the equivalents of JAX-RS playback @HeaderParam

, @FormParam

, @QueryParam

and such?

+3


source to share


3 answers


Inside the activity, you can get the request header using a method request()

, for example 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 API for Java or Scala .

+10


source


This line worked for me:



implicit request => val User-Agent:String = request.headers.get("User-Agent").get

      

+4


source


Better to use constant than hardcoded String in Scala code

import play.mvc.Http

val userAgent: String = request.headers.get(Http.HeaderNames.USER_AGENT).get

      

+1


source







All Articles