Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Play 2.0) Set maximum POST size for AnyContent

I'm using Scala in Play 2.0, and I'm getting a 413 error whenever large data (over 100KB) is POSTed to a particular endpoint. It's using the anyContent parser, and it's not reasonable to use any other parser for this particular API.

There are other questions on Stack Overflow that show how to increase the maximum POST size for text or JSON requests. How do I do it for anyContent, or just increase the limit project-wide?

like image 370
Ben Dilts Avatar asked Jul 27 '12 23:07

Ben Dilts


1 Answers

TL;DR add parsers.text.maxLength = 512k or whatever size to your application.conf

Update: Found in the official documentation

It's actually documented in the API, although it took some digging to actually find it.

Expanding DEFAULT_MAX_TEXT_LENGTH shows that the max size for text data is configurable by setting parsers.text.maxLength in application.conf. Looking in the source itself, the default is 100Kb so this is most likely what you need to set.

On a somewhat related note, we also have the maxLength method which can be used for any BodyParser, which implies for non-text data, there is no upper limit unless you apply that method. In fact, we can apply it to the AnyContent parser like so:

def foo = Action(parse.maxLength(512 * 1024, parser = parse.anyContent)) { implicit req =>
    req.body match {
        case Left(_) => EntityTooLarge
        case Right(body) => Ok("This is totally not too large")
    }
}
like image 81
thatsmydoing Avatar answered Oct 23 '22 21:10

thatsmydoing