I have a problem with flask. There is a api, post method, I just want to get the form data through request.form... throw a 400 bad request. I don't know what's in the form that's causing this exception.
Does anyone know what the cause is? help me
And here is part of the code and error message for the python flask application.
# python:2.7.5
# Werkzeug==0.14.1
from flask import Flask, request_started, request
def request_started_handler(sender, **extra):
request_form = str(request.form)
print(request_form)
app = Flask(__name__)
request_started.connect(request_started_handler, app)
@app.route('/upload')
def hello_world():
api_args = tuple(request.values.lists())
print(api_args)
return '<b>Hello World</b>'
if __name__ == "__main__":
app.run()
File "/data/myproject/app.py", line 50, in _debug_fucntion
request_form = str(request.form)
File "/data/lib/python2.7/site-packages/werkzeug/local.py", line 347, in __getattr__
return getattr(self._get_current_object(), name)
File "/data/lib/python2.7/site-packages/werkzeug/utils.py", line 73, in __get__
value = self.func(obj)
File "/data/lib/python2.7/site-packages/werkzeug/wrappers.py", line 537, in form
self._load_form_data()
File "/data/lib/python2.7/site-packages/flask/wrappers.py", line 168, in _load_form_data
RequestBase._load_form_data(self)
File "/data/lib/python2.7/site-packages/werkzeug/wrappers.py", line 385, in _load_form_data
mimetype, content_length, options)
File "/data/lib/python2.7/site-packages/werkzeug/formparser.py", line 205, in parse
content_length, options)
File "/data/lib/python2.7/site-packages/werkzeug/formparser.py", line 114, in wrapper
exhaust()
File "/data/lib/python2.7/site-packages/werkzeug/wsgi.py", line 1289, in exhaust
self.read(chunk)
File "/data/lib/python2.7/site-packages/werkzeug/wsgi.py", line 1307, in read
return self.on_disconnect()
File "/data/lib/python2.7/site-packages/werkzeug/wsgi.py", line 1275, in on_disconnect
raise ClientDisconnected()
ClientDisconnected: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
I think the problem is related to
request.form
Flask will raise a werkzeug.exceptions.BadRequestKeyError exception when request.form['key'] or the key value doesn't exist in the request.form.
I recommend you to use
request.form.get("something", False)
to avoid that error and you will get None as default value if the key doesn't exist.
Maybe the form tag dosen't contains:
enctype='multipart/form-data'
[Edit] Flask sets request.data or request.form for POST requests. Both are dictionaries. For parameters embedded within the POST request as a form:
request_form_dict = request.form
for key in request_form_dict :
print ('form key is :'+request_form_dict[key])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.