• Register
0 votes
461 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')

 

7 5 2
3,870 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')

 

9 7 4
38,600 points

Related questions

0 votes
1 answer 2 views
2 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
0 votes
0 answers 86 views
86 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
1 answer 6 views
6 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 17 views
17 views
Problem: warning: do not use the development server in a production environment.
asked Feb 12 Dan phillip 4.7k points
1 vote
1 answer 2K views
2K views
Problem: I installed flask in my system by running the command: pip install flask Collecting flask Downloading https://files.pythonhosted.org/packages/f2/28/2a03252dfb9ebf377f40fba6a7841b47083260bf8bd8e737b0c6952df83f/Flask-1.1.2-py2.py3-none-any.whl (94kB) |████████████████████████████████| ... operable program or batch file. Whats the problem running this can any one tell me? Thanks in advance.
asked May 12, 2020 samhaz 5k points
1 vote
1 answer 321 views
321 views
Problem : I am getting bellow flask app error error: could not locate flask application. you did not provide the flask_app environment variable.
asked Nov 8, 2019 peterlaw 6.9k points
0 votes
1 answer 27 views
27 views
Problem : your wsgipath refers to a file that does not exist.
asked Feb 15 Shovo210 13.3k points
0 votes
1 answer 7 views
7 views
Problem: Why I am getting this error: Typeerror: 'str' does not support the buffer interface
asked Mar 16 ummesalma 24.5k points
0 votes
1 answer 12 views
12 views
Problem : valueerror: view function did not return a response.
asked Feb 18 Shovo210 13.3k points
0 votes
1 answer 1.1K views
1.1K views
Problem : I am trying to deploy my Python Flask application in my Azure web app. I had created web app(Flask) and published the code. After publishing it, I am facing below error from the site. The page cannot be displayed because an internal server error ... . But this was the case only in my subscription(my free subscription with MSDN). But it was working fine in the Organisation subscription.
asked Dec 16, 2019 alecxe 7.5k points