I'm building a Flask web application where users can record and submit 30 seconds of audio (WebM format) through a form. However, when I try to submit a 30-second recording (~650 KB), I get a 413 Request Entity Too Large error.
I've tried the following:
Set MAX_CONTENT_LENGTH in Flask:
app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024 # 100MB limit
Running Flask with Waitress:
from waitress import serveserve
(app, host="127.0.0.1", port=5000, max_request_body_size=100 * 1024 * 1024)
Testing Gunicorn with gunicorn_config.py:
limit_request_field_size = 0
limit_request_line = 0
timeout = 300
worker_connections = 1000
Despite these changes, submitting larger requests (30 seconds of audio) still triggers the 413 error.
Here’s what I’ve ruled out:
- Small audio files (~100 KB) work fine.
- MacOS firewall is disabled.
- I'm not using Nginx.
Code Snippets
Frontend (JavaScript) – Recording and Submitting Audio
function processRecording() {
let audioBlob = new Blob(audioChunks, { type: 'audio/webm' });
let reader = new FileReader();
reader.readAsDataURL(audioBlob);
reader.onloadend = function () {
audioDataInput.value = reader.result;
document.querySelector('form').submit();
};
}
Backend (Flask Route for Submitting Audio)
@app.route('/speaking_task_submit', methods=['POST'])
def speaking_task_submit():
print(f"Flask MAX_CONTENT_LENGTH: {app.config.get('MAX_CONTENT_LENGTH')} bytes")
print(f"Received Content-Length: {request.content_length} bytes")
if request.content_length and request.content_length > app.config.get('MAX_CONTENT_LENGTH'):
return jsonify({"error": "Request size exceeded!"}), 413
audio_data = request.form['audio_data']
if not audio_data or "," not in audio_data:
return jsonify({"error": "Invalid or missing audio data"}), 400
# Save audio as .webm
audio_content = audio_data.split(",")[1]
audio_file_path = f"uploads/candidate_audio.webm"
with open(audio_file_path, "wb") as audio_file:
audio_file.write(base64.b64decode(audio_content))
return jsonify({"message": "Audio received successfully"})
Environment Details:
- MacOS Monterey
- Python 3.10
- Flask 3.1.0
- Waitress 3.0.2
- Gunicorn 23.0.0
What I’ve Tried:
- Running Flask with Waitress and setting max_request_body_size=100 * 1024 * 1024.
- Running Flask with Gunicorn and setting limit_request_field_size = 0.
- Disabled any firewall or proxy limits on MacOS.
Question:
- What could still be limiting the request size?
- Is there another hidden limit in Waitress, Gunicorn, or Flask that I’m missing?
- Should I configure any additional middleware settings in Flask?
Any help is greatly appreciated!