• Register
0 votes
522 views

Problem :

I am trying to do the file upload & enter the data task into my MongoDB using a flask but I had below error when I filled my form & upload my image:

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

 My Python Code:

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

 

3.9k points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

Your error is resulting from the BadRequestKeyError because of access to the key that doesn't exist in your 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 the request.files and not the request.form dictionary. Also, you need to use the loop as the value keyed under u_img is the instance of FileStorage and not at all iterable.

Please refer below updated code that will help you in resolving all of the issues.

@app.route('/', methods=['GET', 'POST'])
def index():
    mytarget = os.path.join(app_root, 'static/img/')
    if not os.path.isdir(mytarget):
        os.makedirs(mytarget)
    if request.method == 'POST':
        ...
        file = request.files['u_img']
        file_name = file.myfilename or ''
        mydestination = '/'.join([mytarget, file_name])
        file.save(mydestination)
        ...
    return render_template('index.html')

 

38.6k points

Related questions

0 votes
1 answer 181 views
181 views
Problem: I'm trying to do a file upload & enter data task into my MongoDB using flask but I had this error when I filled my form & upload the image: Bad Request The browser (or proxy) sent a request that this server could not understand. my HTML code ... , 'email': email, 'img_name': file_name}) return render_template('index.html') else: return render_template('index.html') app.run(debug=True)
asked Apr 30 muktaa 34.6k points
0 votes
1 answer 18 views
18 views
Problem: What are my options for dealing with this issue? bad request your browser sent a query this server could not understand.
asked Apr 1 rakib1 51.5k points
1 vote
1 answer 26 views
26 views
I am getting this message from my browser: Your browser sent a request that this server could.
asked May 18 PkGuy 25.9k points
0 votes
0 answers 182 views
182 views
I have a dataframe of almost 120000 records as follows. Also I have a mongoDB collection which looks exacly same as below dataframe ItemID ParentID ItemRating ItemPrice Qty A1 ItemA1 0 12 100 A2 ItemA2 0 15 200 B1 ItemB1 0 20 300 B2 ItemB2 0 25 400 B3 ItemB3 0 30 ... PyMongo update_many method by setting upsert=true. but I am not sure how can I do that ? how should I write my filter condition ?
asked Sep 13, 2020 NguyenTram 1k points
0 votes
0 answers 48 views
48 views
Problem: Although I am by no means an expert on Ubuntu, I have had two servers running for a couple of years with no problems. Last night, when attempting to access a local website on one of them I got the error: **Bad Request** Your browser sent a ... Request error was encountered while trying to use an ErrorDocument to handle the request.I'm about out of ideas, and looking for any suggestions.
asked May 1 Moumita Bhowmick 12.9k points
0 votes
1 answer 19 views
19 views
Problem: how to Solve this: flask pymongo HELP! I need help understanding this: flask pymongo Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 13 Mashhoodch 13k points
0 votes
1 answer 7 views
7 views
Problem: Nameerror: global name 'request' is not defined
asked Jul 5 nila 121k points
0 votes
1 answer 19 views
19 views
Problem: Give me an answer please : Sqlite objects created in a thread can only be used in that same thread
asked Jun 17 asha 121k points
0 votes
1 answer 16 views
16 views
Problem: Is there somebody who can assist : Sqlite objects created in a thread can only be used in that same thread?
asked Jun 16 Mezbah 130k points
0 votes
1 answer 17 views
17 views
Problem: Is there anyone who can help me find a solution : Error: could not locate flask application. you did not provide the flask_app environment variable?
asked Jun 10 Mezbah 130k points