How to get request size in Play 2.0.x?

In Play Framework 2.0.3 (scala), how do you determine the size (in bytes) of any request [_]?

We are trying to get this information for logging purposes.

We expect some value from request.body.asRaw, but always get None:

def logRawRequest[A](request: Request[A]) {
  request.body match {
    case c: AnyContent => println("Raw: "+c.asRaw)
  }
}

There must be something simple that we are missing, right?


Thanks for the helpful answers! It turns out that the Content-Length header is present only for POST / PUT, so we use it for them and return to the request length for GET / DELETE, for example:

val requestSize = request.method match {
  case "POST" | "PUT" => request.headers.get(CONTENT_LENGTH).map(_.toInt).getOrElse(-1)
  case _ => request.toString().length
}
+5
source share
1 answer

I have not tried this, but maybe

request.headers.get(play.api.http.HeaderNames.CONTENT_LENGTH)

would do the trick? At least this will work for simple POST requests. It just gives the body length.

GET , - , , request.uri.length , ( cookie). , POST.

+3

All Articles