Skip to content

Return a 413 instead of 400 when data limit is exceeded #972

@Nilix007

Description

@Nilix007

Hey there,

when sending a too large JSON payload (i.e. larger than the specified limit) Rocket will show a missleading error message in the debug log and respond with 400 Bad Request.

Instead of this behaviour, I'd expect Rocket to show a error message mentioning the too large payload and respond with 413 Payload Too Large.

Rocket version: 0.4.0
OS: Debian buster (10.0)

Code:

#![feature(proc_macro_hygiene, decl_macro)]
use rocket::*;
use rocket_contrib::json::Json;

#[post("/", data = "<data>")]
fn x(data: Json<String>) {}

fn main() {
    ignite().mount("/", routes![x]).launch();
}
$ curl -H Content-Type:\ application/json -d @really-large-json-file http://localhost:8000

            <!DOCTYPE html>
            <html>
            <head>
                <meta charset="utf-8">
                <title>400 Bad Request</title>
            </head>
            <body align="center">
                <div align="center">
                    <h1>400: Bad Request</h1>
                    <p>The request could not be understood by the server due
                to malformed syntax.</p>
                    <hr />
                    <small>Rocket</small>
                </div>
            </body>
            </html>
🔧 Configured for development.
    => address: localhost
    => port: 8000
    => log: normal
    => workers: 8
    => secret key: generated
    => limits: forms = 32KiB
    => keep-alive: 5s
    => tls: disabled
🛰  Mounting /:
    => POST / (x)
🚀 Rocket has launched from http://localhost:8000
POST / application/json:
    => Matched: POST / (x)
    => Warning: Data left unread. Force closing network stream.
    => Error: Couldn't parse JSON body: Error("EOF while parsing a string", line: 1, column: 1048576)
    => Outcome: Failure
    => Warning: Responding with 400 Bad Request catcher.
    => Response succeeded.

Best,
Felix

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementA minor feature requestrequestRequest for new functionality

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions