Skip to content

Customising the maximum size of request payload #5704

Closed
@mykola-mokhnach

Description

🐣 Is your feature request related to a problem? Please describe.

I would like to explicitly limit the maximum size of an incoming json body for a POST request.

💡 Describe the solution you'd like

Currently I see there is a private _client_max_size property for such purpose, which equals to 4MB by default. Although, I don't see a clear way to, for example, limit the maximum size of a json object that one retrieves from await request.json() call.

Describe alternatives you've considered

Currently the following workaround is used:

        default_client_max_size = request._client_max_size
        request._client_max_size = MAX_PAYLOAD_SIZE
        try:
            req_json = await request.json()
        except web_exceptions.HTTPClientError:
            raise
        except Exception:
            raise web.HTTPBadRequest(reason='The request body is expected to be a valid JSON')
        finally:
            request._client_max_size = default_client_max_size

Although, it would be nice if _client_max_size would be public/had a setter, so one could limit the body size in request.text() or request.json() APIs without changing values of private properties.

📋 Additional context

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions