Flask – Bad Request The browser (or proxy) sent a request that this server could not understand [duplicate]

The error there is resulting from a BadRequestKeyError because of access to a key that doesn’t exist in request.form.

ipdb> request.form['u_img']
*** BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.

Uploaded files are keyed under request.files and not request.form dictionary. Also, you need to lose the loop because the value keyed under u_img is an instance of FileStorage and not iterable.

@app.route("https://stackoverflow.com/", methods=['GET', 'POST'])
def index():
    target = os.path.join(app_root, 'static/img/')
    if not os.path.isdir(target):
        os.makedirs(target)
    if request.method == 'POST':
        ...
        file = request.files['u_img']
        file_name = file.filename or ''
        destination = "https://stackoverflow.com/".join([target, file_name])
        file.save(destination)
        ...
    return render_template('index.html')

Leave a Comment