26

Using Python's module bottle, I'm getting HTTP 413 error when posting requests of body size > bottle's internal MEMFILE_MAX constant. Minimal working example is shown below.

Server part (server.py):

from bottle import *

@post('/test')
def test():
    return str(len(request.forms['foo']));

def main():
    run(port=8008);

if __name__ == '__main__':
    main();

Client part (client.py):

import requests

def main():
    url = 'http://127.0.0.1:8008/test';

    r = requests.post(url, data={ 'foo' : 100000 * 'a' });
    print(r.text);

    r = requests.post(url, data={ 'foo' : 200000 * 'a' });
    print(r.text);

if __name__ == '__main__':
    main();

The first request prints:

100000

The second request prints:

...
<body>
    <h1>Error: 413 Request Entity Too Large</h1>
    <p>Sorry, the requested URL <tt>&#039;http://127.0.0.1:8008/test&#039;</tt>
       caused an error:</p>
    <pre>Request to large</pre>
</body>
....

I have absolutely no idea how to increase the bottle's internal limit. Is there any simple way to increase the limit, allowing requests of size, e.g., 1 MB?

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
Tregoreg
  • 18,872
  • 15
  • 48
  • 69
  • 1
    Try changing `bottle.BaseRequest.MEMFILE_MAX` to something larger than `102400`. – Blender May 31 '13 at 21:15
  • 1
    I love that number 102,400. It is so meaningful. – vy32 Jan 15 '20 at 22:25
  • @vy32 Actually it's a bit less arbitrary than it [looks](https://github.com/bottlepy/bottle/commit/cb0cacd602c7fdf1a63a44299a206ab6acf8dc57#diff-ad8cb2f640fd3a70db3fc97f3044a4e6R1360). – x-yuri Jul 04 '20 at 16:50
  • Thanks. That's really funny. `#TODO Should not be hard coded...`. And now it no longer is! – vy32 Jul 04 '20 at 20:19

1 Answers1

55

You should be able to just

import bottle
bottle.BaseRequest.MEMFILE_MAX = 1024 * 1024 # (or whatever you want)

This appears to be the only way based on the source

sberry
  • 128,281
  • 18
  • 138
  • 165
  • 1
    Exactly solves my problem! I had a suspicion that I'll have to access the ``MEMFILE_MAX`` property somehow, but wasn't sure at all. Thanks! – Tregoreg May 31 '13 at 21:29
  • 1
    And another stack overflow-user saved my day :) Just for the sake of matter: value comes in byte, standard size is 102400 (as the source from your link also mentioned) – Michael P Jun 16 '15 at 16:06
  • you sir, made my day – pyInTheSky Apr 13 '17 at 18:23