In Play Framework 2.0.3 (scala), how do you determine the size (in bytes) of any Request[_]?
We're trying to obtain this information for logging purposes.
We would expect some value from request.body.asRaw, but we always obtain None:
def logRawRequest[A](request: Request[A]) {
request.body match {
case c: AnyContent => println("Raw: "+c.asRaw)
}
}
There must be something simple that we're missing, right?
Thanks for the helpful answers! It turns out that the Content-Length header is only present for POST/PUT, so we use it for those, and fallback to the length of the query for GET/DELETE, like this:
val requestSize = request.method match {
case "POST" | "PUT" => request.headers.get(CONTENT_LENGTH).map(_.toInt).getOrElse(-1)
case _ => request.toString().length
}