简体   繁体   English

python flask request.form 400 错误请求

[英]python flask request.form 400 Bad Request

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.我flask有问题,有一个api,post方法,我只想通过request.form获取表单数据...抛出一个400错误的请求。 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 flask 应用程序的部分代码和错误消息。

# 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. Flask 将在request.form['key']或 request.form 中不存在键值时引发 werkzeug.exceptions.BadRequestKeyError 异常。

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.为避免该错误,如果密钥不存在,您将获得 None 作为默认值。

Maybe the form tag dosen't contains:也许表单标签不包含:

enctype='multipart/form-data'

[Edit] Flask sets request.data or request.form for POST requests. [编辑] Flask 为 POST 请求设置 request.data 或 request.form。 Both are dictionaries.两者都是字典。 For parameters embedded within the POST request as a form:对于作为表单嵌入在 POST 请求中的参数:

request_form_dict = request.form
for key in request_form_dict :
    print ('form key is :'+request_form_dict[key])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM