Solve ‘Bad Request The browser (or proxy) sent a request that this server could not understand’

Are you designing a project for a web page or web application? You must have experienced a ‘Bad Request The browser (or proxy) sent a request that this server could not understand’ error, which obviously sees a long type of error that is hard to tackle. Well, there is nothing like that, no matter how difficult or complex an error can look, it can be solved.

Let’s dig into the error to get the proper solution to fix this error. First, you need to look at the error that shows up

Error to Pop UP

This is how you get the error of warning

HTML Code

<form class="form-check form-control" method="post" enctype="multipart/form-data" action="{{ url_for('index') }}">      
          <label>Full Name*</label></td>
          <input name="u_name" type="text" class="text-info my-input" required="required" />
          <label>Email*</label>
          <input name="u_email" type="email" class="text-info my-input" required="required" />
          <label>Password*</label>
          <input name="u_pass" type="password" class="text-info my-input" required="required" />
          <label>Your Image*</label>
          <input name="u_img" type="file" class="text-info" required="required" /></td>
          <input name="btn_submit" type="submit" class="btn-info" />
    </form>

Python Version

from flask import Flask, render_template, request, url_for
from flask_pymongo import PyMongo
import os
app = Flask(__name__)
app.config['MONGO_DBNAME'] = 'flask_assignment'
app.config['MONGO_URI'] = 'mongodb://<user>:<pass>@<host>:<port>/<database>'
mongo = PyMongo(app)
app_root = os.path.dirname(os.path.abspath(__file__))


@app.route('/', methods=['GET', 'POST'])
def index():
    target = os.path.join(app_root, 'static/img/')
    if not os.path.isdir(target):
        os.mkdir(target)
    if request.method == 'POST':
        name = request.form['u_name']
        password = request.form['u_pass']
        email = request.form['u_email']
        file_name = ''
        for file in request.form['u_img']:
            file_name = file.filename
            destination = '/'.join([target, file_name])
            file.save(destination)
        mongo.db.employee_entry.insert({'name': name, 'password': password, 'email': email, 'img_name': file_name})
        return render_template('index.html')
    else:
        return render_template('index.html')

app.run(debug=True)

When you try to work on such projects, the error shows up. Like this:

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

The error occurs because of the BadRequestKeyError that is used to access a key that has no existence within the request.form. Take a look at the errSor

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

Now let’s have a look at the solution to properly fix the error

Solution to fix ‘Bad Request The browser (or proxy) sent a request that this server could not understand’

There is a simple solution to resolve the error in an efficient and effective way. You just need to upload the key files under the request.files dictionary, but you need to make sure it is not under the request.form. Remove the loops as the key values under iterable u-img.

You need to use the code below to tackle the error ‘Bad Request The browser (or proxy) sent a request that this server could not understand’

@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')

Conclusion

And that is exactly how you can rid of ‘Bad Request The browser (or proxy) sent a request that this server could not understand’.

You just need to implement the code to get it resolved.

Leave a Reply

Your email address will not be published. Required fields are marked *