Scala play http filters: how to find the request body

自闭症网瘾萝莉.ら 提交于 2019-11-29 04:10:12

I spent some time on this. I am no means a Scala expert but this works pretty well! :)

object AccessLog extends EssentialFilter {
  def apply(nextFilter: EssentialAction) = new EssentialAction {
    def apply(requestHeader: RequestHeader) = {
      val startTime = System.currentTimeMillis

      nextFilter(requestHeader).map { result =>
        val endTime = System.currentTimeMillis
        val requestTime = endTime - startTime

        val bytesToString: Enumeratee[ Array[Byte], String ] = Enumeratee.map[Array[Byte]]{ bytes => new String(bytes) } 
        val consume: Iteratee[String,String] = Iteratee.consume[String]()    
        val resultBody : Future[String] = result.body |>>> bytesToString &>> consume

        resultBody.map {
          body =>
            Logger.info(s"${requestHeader.method} ${requestHeader.uri}" +
          s" took ${requestTime}ms and returned ${result.header.status}")
            val jsonBody = Json.parse(body)
            Logger.debug(s"Response\nHeader:\n${result.header.headers.toString}\nBody:\n${Json.prettyPrint(jsonBody)}")
        }

         result.withHeaders("Request-Time" -> requestTime.toString)

      }
    }
  }

The end result will print the body as a Json String (pretty printed).

First thing you have to know is when the Filter is invoked, the request body is not parsed yet. That's why it's giving you a RequestHeader. You'll have to find out the type of body, and call the proper body parser accordingly.

You can find a example of body parsing in the CSRF filter (It can lookup for CSRF tokens in the first bytes of the request body).

See: https://github.com/playframework/playframework/blob/master/framework/src/play-filters-helpers/src/main/scala/csrf.scala#L221-L233.

Hope it helps.

In the controller method that routes to the action, simply call

Map<String, String[]> params = request().queryString();

This will get you a map of parameters, where you can then call

params.get("someParam")[0] 

to get the param (if it is a single value). If the param is a list, ignore the indexing andthat will return an array.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!