An HTTP request is a header followed by a body. The header is typically small - it can be safely buffered in memory, hence in Play it is modelled using the RequestHeader class. The body however can be potentially very long, and so is not buffered in memory, but rather is modelled as a stream. However, many request body payloads are small and can be modelled in memory, and so to map the body stream to an object in memory, Play provides a BodyParser abstraction.

Since Play is an asynchronous framework, the traditional InputStream can’t be used to read the request body - input streams are blocking, when you invoke read, the thread invoking it must wait for data to be available. Instead, Play uses an asynchronous streaming library called Akka Streams. Akka Streams is an implementation of Reactive Streams, a SPI that allows many asynchronous streaming APIs to seamlessly work together, so though traditional InputStream based technologies are not suitable for use with Play, Akka Streams and the entire ecosystem of asynchronous libraries around Reactive Streams will provide you with everything you need.

First we see that there is a generic type A, and then that an action must define a BodyParser[A]. With Request[A] being defined as:

trait Request[+A] extends RequestHeader {
def body: A
}

The A type is the type of the request body. We can use any Scala type as the request body, for example String, NodeSeq, Array[Byte], JsonValue, or java.io.File, as long as we have a body parser able to process it.

To summarize, an Action[A] uses a BodyParser[A] to retrieve a value of type A from the HTTP request, and to build a Request[A] object that is passed to the action code.

Most typical web apps will not need to use custom body parsers, they can simply work with Play’s built in body parsers. These include parsers for JSON, XML, forms, as well as handling plain text bodies as Strings and byte bodies as ByteString.

The default body parser that’s used if you do not explicitly select a body parser will look at the incoming Content-Type header, and parses the body accordingly. So for example, a Content-Type of type application/json will be parsed as a JsValue, while a Content-Type of application/x-www-form-urlencoded will be parsed as a Map[String, Seq[String]].

The default body parser produces a body of type AnyContent. The various types supported by AnyContent are accessible via as methods, such as asJson, which returns an Option of the body type:

The default body parser tries to determine if the request has a body before it tries to parse. According to the HTTP spec, the presence of either the Content-Length or Transfer-Encoding header signals the presence of a body, so the parser will only parse if one of those headers is present, or on FakeRequest when a non-empty body has explicitly been set.

If you would like to try to parse a body in all cases, you can use the anyContent body parser, described below.

Note this time that the type of the body is JsValue, which makes it easier to work with the body since it’s no longer an Option. The reason why it’s not an Option is because the json body parser will validate that the request has a Content-Type of application/json, and send back a 415 Unsupported Media Type response if the request doesn’t meet that expectation. Hence we don’t need to check again in our action code.

This of course means that clients have to be well behaved, sending the correct Content-Type headers with their requests. If you want to be a little more relaxed, you can instead use tolerantJson, which will ignore the Content-Type and try to parse the body as json regardless:

In the previous example, all request bodies are stored in the same file. This is a bit problematic isn’t it? Let’s write another custom body parser that extracts the user name from the request Session, to give a unique file for each user:

Note: Here we are not really writing our own BodyParser, but just combining existing ones. This is often enough and should cover most use cases. Writing a BodyParser from scratch is covered in the advanced topics section.

Text based body parsers (such as text, json, xml or formUrlEncoded) use a max content length because they have to load all the content into memory. By default, the maximum content length that they will parse is 100KB. It can be overridden by specifying the play.http.parser.maxMemoryBuffer property in application.conf:

play.http.parser.maxMemoryBuffer=128K

For parsers that buffer content on disk, such as the raw parser or multipart/form-data, the maximum content length is specified using the play.http.parser.maxDiskBuffer property, it defaults to 10MB. The multipart/form-data parser also enforces the text max length property for the aggregate of the data fields.

The signature of this function may be a bit daunting at first, so let’s break it down.

The function takes a RequestHeader. This can be used to check information about the request - most commonly, it is used to get the Content-Type, so that the body can be correctly parsed.

The return type of the function is an Accumulator. An accumulator is a thin layer around an Akka StreamsSink. An accumulator asynchronously accumulates streams of elements into a result, it can be run by passing in an Akka Streams Source, this will return a Future that will be redeemed when the accumulator is complete. It is essentially the same thing as a Sink[E, Future[A]], in fact it is nothing more than a wrapper around this type, but the big difference is that Accumulator provides convenient methods such as map, mapFuture, recover etc. for working with the result as if it were a promise, where Sink requires all such operations to be wrapped in a mapMaterializedValue call.

The accumulator that the apply method returns consumes elements of type ByteString - these are essentially arrays of bytes, but differ from byte[] in that ByteString is immutable, and many operations such as slicing and appending happen in constant time.

The return type of the accumulator is Either[Result, A] - it will either return a Result, or it will return a body of type A. A result is generally returned in the case of an error, for example, if the body failed to be parsed, if the Content-Type didn’t match the type that the body parser accepts, or if an in memory buffer was exceeded. When the body parser returns a result, this will short circuit the processing of the action - the body parsers result will be returned immediately, and the action will never be invoked.

In rare circumstances, it may be necessary to write a custom parser using Akka Streams. In most cases it will suffice to buffer the body in a ByteString first, this will typically offer a far simpler way of parsing since you can use imperative methods and random access on the body.

However, when that’s not feasible, for example when the body you need to parse is too long to fit in memory, then you may need to write a custom body parser.

import play.api.mvc.BodyParser
import play.api.libs.streams._
import akka.util.ByteString
import akka.stream.scaladsl._
val csv: BodyParser[Seq[Seq[String]]] = BodyParser { req =>
// A flow that splits the stream into CSV lines
val sink: Sink[ByteString, Future[Seq[Seq[String]]]] = Flow[ByteString]
// We split by the new line character, allowing a maximum of 1000 characters per line
.via(Framing.delimiter(ByteString("\n"), 1000, allowTruncation = true))
// Turn each line to a String and split it by commas
.map(_.utf8String.trim.split(",").toSeq)
// Now we fold it into a list
.toMat(Sink.fold(Seq.empty[Seq[String]])(_ :+ _))(Keep.right)
// Convert the body to a Right either
Accumulator(sink).map(Right.apply)
}