(Play 2.0) Set maximum POST size for AnyContent
Asked Answered
S

3

15

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?

Smog answered 27/7, 2012 at 23:4 Comment(0)
H
22

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")
    }
}
Hairbreadth answered 29/7, 2012 at 11:0 Comment(3)
Why is it 1024 instead of just 1000?Michaelson
a kilobyte is usually defined as 2^10 or 1024 bytes instead of just 1000Hairbreadth
Thanks, this helped! Also, Play 2.4 warns: parsers.text.maxLength is deprecated, use play.http.parser.maxMemoryBuffer insteadAdhern
F
0

Note for those using the raw parser, that one takes maximum as a parameter:

Action(parse.raw(10000 * 1024))

See the parse.raw method for more details.

Fernyak answered 21/12, 2017 at 17:10 Comment(0)
R
0

The raw body parser does not buffer the whole body into memory, it buffers the body up to a point (configured using play.http.parser.maxMemoryBuffer, defaults to 100kb), and once that is exceeded, it flushes the body to a file and starts writing it to the file, but it also has a limit on how much data it will write to a file, which is configured using play.http.parser.maxDiskBuffer, and this defaults to 10mb. Your 15mb body is probably exceeding that limit, so you need to increase play.http.parser.maxDiskBuffer accordingly. This is all explained in the documentation.

To answer your actual question on how to stream requests, documentation for writing custom body parsers in Java is here:

https://www.playframework.com/documentation/2.6.x/JavaBodyParsers#Writing-a-custom-body-parser

That explains most things you need to know about how Play body parsers work and therefore how to do streaming body parsing, no point in duplicating that documentation in this answer.

Roselynroseman answered 15/12, 2019 at 9:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.