Reputation: 75
For the following piece of code given in the Play documentation (https://www.playframework.com/documentation/2.6.x/ScalaLogging)
class AccessLoggingAction @Inject() (parser: BodyParsers.Default)(implicit ec: ExecutionContext) extends ActionBuilderImpl(parser) {
val accessLogger = Logger("access")
override def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[Result]) = {
accessLogger.info(s"method=${request.method} uri=${request.uri} remote-address=${request.remoteAddress}")
block(request)
}
}
The above code uses BodyParsers.Default
, how would I use a TolerantText BodyParser instead? It doesn't appear that BodyParsers.TolerantText
exists?
I'd like to use a TolerantText BodyParser so that I can log an error in the event that a POST request is submitted such that the request specifies a content type of JSON and sends an invalid JSON body such as {"---"} (The Default BodyParser throws an error immediately when an invalid JSON is given which doesn't give me the opportunity to log that error. I've read that a TolerantText BodyParser in Play doesn't throw the error immediately because it ignores the content header and doesn't try to parse the body based on the content header.)
I'd like to be able to use a TolerantText body parser or anything that doesn't immediately throw an error so I can log when that happens in that particular action.
Upvotes: 0
Views: 652
Reputation: 19527
To inject PlayBodyParsers
as @rethab suggested, define your class as follows:
class AccessLoggingAction @Inject() (parser: PlayBodyParsers)(implicit ec: ExecutionContext)
extends ActionBuilderImpl(parser.tolerantText) {
...
}
Upvotes: 2