Skip to content

Instantly share code, notes, and snippets.

@schmitch
Created October 25, 2015 11:52
Show Gist options
  • Select an option

  • Save schmitch/b253eb8455eb3e06bfdc to your computer and use it in GitHub Desktop.

Select an option

Save schmitch/b253eb8455eb3e06bfdc to your computer and use it in GitHub Desktop.
Scala Filter
import javax.inject.Inject
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.json.{JsObject, JsString, Json}
import play.api.mvc._
import scala.concurrent.Future
class BodyFilter @Inject()() extends Filter {
def apply(nextFilter: RequestHeader => Future[Result])
(requestHeader: RequestHeader): Future[Result] = {
nextFilter(requestHeader).map { result =>
val x = result.body.map { data =>
val h = Json.parse(data).asInstanceOf[JsObject] + ("server" -> JsString("1.0"))
h.toString().getBytes
}
result.copy(body = x)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment