Surprisingly, I couldn't much info about others having the same issue.
To my understanding, whenever file exceeds MAX_CONTENT_LENGTH
, it automatically throws the 413. The question is how do I catch it?
For some odd reason, I can catch 415 status codes AND custom 413 messages on the client side (haven't tested with other status codes), but I can't catch ones specifically from RequestEntityTooLarge.
I set the max size as such: app.config["MAX_CONTENT_LENGTH"] = 5242880
My Flask code:
def convert():
try:
data = request.form
fileUpload = request.files["fileUpload"]
# maybe some other code
except RequestEntityTooLarge as e:
print ("- File too large!")
# I can never catch this
return jsonify("large_file"), 413
if file not in valid_formats:
# this is always caught on client, even if I change this status code to 413
# return jsonify("bad_format"), 413
return jsonify("bad_format"), 415
Client side (vanilla JS, with Fetch API)
fetch("/api/convert", {
method: "POST",
body: formData
}).then(response => {
console.log(response.status);
// do whatever based on status
// here, i can catch 415 and custom 413 messages
return response.json();
}).then(data => {
// do some stuff
}).catch(error => {
console.log(error);
})
In the network tab in my browser, I can see that if I send a custom 413 message (return jsonify("bad_format"), 413
, it shows that it received status code 413.
custom 413 message
However, when I try to send 413 message when catching RequestEntityTooLarge, there's no status code...?
with RequestEntityToLarge
I tried it in Firefox and Chrome, with both the Flask production server, and Gunicorn server. Such odd behaviour I do not understand!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…