close

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

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Flask – Bad Request The browser (or proxy) sent a request that this server could not understand in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Flask – Bad Request The browser (or proxy) sent a request that this server could not understand Error Occurs?

Today I get the following error Flask – Bad Request The browser (or proxy) sent a request that this server could not understand in python.

How To Solve Flask – Bad Request The browser (or proxy) sent a request that this server could not understand Error ?

  1. How To Solve Flask – Bad Request The browser (or proxy) sent a request that this server could not understand Error ?

    To Solve Flask – Bad Request The browser (or proxy) sent a request that this server could not understand Error The error there is resulting from a BadRequestKeyError because of access to a key that doesn't exist in request.form.

  2. Flask – Bad Request The browser (or proxy) sent a request that this server could not understand

    To Solve Flask – Bad Request The browser (or proxy) sent a request that this server could not understand Error The error there is resulting from a BadRequestKeyError because of access to a key that doesn't exist in request.form.

Solution 1

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('/', 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 = '/'.join([target, file_name])
        file.save(destination)
        ...
    return render_template('index.html')

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read