0

I want to be able to check for the size of uploaded files. And if it exceeds a limit, i want to be able to issue my REST API errors.

I have this code:

app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024

If you try to upload a file that is larger than 1MB, the application will now refuse it.

But this does not give me a lot of control, in terms of the REST API message that my app will generate.

So how can i check for max size of uploaded file, and issue my personal message, along with an HTTP status code, whenever that happens?

EDIT: Now i receive this on my curl terminal (client side)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>413 Request Entity Too Large</title>
<h1>Request Entity Too Large</h1>
<p>The data value transmitted exceeds the capacity limit.</p>

While i would like to send something like this (server side code):

return make_response(json.dumps({'error_message': 'file size too large'}), 413)
user1584421
  • 3,499
  • 11
  • 46
  • 86
  • I think you can refer to this post for your answer https://stackoverflow.com/questions/62869992/flask-file-size-limit-413-error-closes-connection . – Babapt Feb 15 '21 at 14:45
  • @Babapt thank you but i do not want to redirect. I want to transmit my own REST API error message. Please look at the edit on my question. – user1584421 Feb 15 '21 at 14:49
  • Found it. Check the answer but thanks! – user1584421 Feb 15 '21 at 14:52

1 Answers1

0

Solved it with this:

@app.errorhandler(413)
def request_entity_too_large(error):
    return make_response(json.dumps({'error_message': 'file size too large'}), 413)

Added error handler

user1584421
  • 3,499
  • 11
  • 46
  • 86