See the Play documentation for combining body parsers:
http://www.playframework.com/documentation/2.2.0/ScalaBodyParsers
If you want to limit the message body to xml or json only, you can write something along these lines:
val xmlOrJson = parse.using { request => request.contentType.map(_.toLowerCase(Locale.ENGLISH)) match { case Some("application/json") | Some("text/json") => play.api.mvc.BodyParsers.parse.json case Some("application/xml") | Some("text/xml") => play.api.mvc.BodyParsers.parse.xml case _ => play.api.mvc.BodyParsers.parse.error(Future.successful(UnsupportedMediaType("Invalid content type specified"))) } } def test = Action(xmlOrJson) { request => request.body match { case json: JsObject => Ok(json)
The xmlOrJson function scans the request header for a content type to determine the body parser. If it is not xml or json, it returns an error body parser with UnsupportedMediaType response (HTTP 415).
You then pass this function as a parser to any action that you want to limit to the contents of xml or json. Inside the action, you can look at the body to determine if xml or json is parsed and processed accordingly.
source share