Python 如何使用flask访问表单数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15855921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to access form data using flask?
提问by daydreamer
My loginendpoint looks like
我的login端点看起来像
@app.route('/login/', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
print request.form # debug line, see data printed below
user = User.get(request.form['uuid'])
if user and hash_password(request.form['password']) == user._password:
login_user(user, remember=True) # change remember as preference
return redirect('/home/')
else:
return 'GET on login not supported'
When I test this using curl, the GETcall looks like
当我使用 测试这个时curl,GET调用看起来像
? ~PYTHONPATH ? ? 43± ? curl http://127.0.0.1:5000/login/
GET on login not supported
but on POST, I am not able to access the form data and get HTTP 400
但是POST,我无法访问表单数据并获取HTTP 400
? ~PYTHONPATH ? ? 43± ? curl -d "{'uuid': 'admin', 'password': 'admin'}" http://127.0.0.1:5000/login/
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>
On the server though, my debug information prints the following
虽然在服务器上,我的调试信息打印以下内容
ImmutableMultiDict([("{'uuid': 'admin', 'password': 'admin'}", u'')])
where I do print request.form. I am not able to understand where I am doing wrong
我在哪里print request.form。我无法理解我哪里做错了
采纳答案by janos
You are not using curlcorrectly. Try like this:
你没有curl正确使用。像这样尝试:
curl -d 'uuid=admin&password=admin'
The 400 Bad Requesterror is the usual behavior when you try to get nonexistent keys from request.form.
该400 Bad Request错误是通常的行为,当你试图从不存在的钥匙request.form。
Alternatively, use request.jsoninstead of request.formand call curllike this:
或者,使用request.json代替request.form并curl像这样调用:
curl -d '{"uuid":"admin","password":"admin"}' -H "Content-Type: application/json"
回答by Blender
You still need to return a response:
您仍然需要返回响应:
from flask import abort
@app.route('/login/', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
user = User.get(request.form['uuid'])
if user and hash_password(request.form['password']) == user._password:
login_user(user, remember=True)
return redirect('/home/')
else:
return abort(401) # 401 Unauthorized
else:
return abort(405) # 405 Method Not Allowed
Here's the documentation for custom Flask error pages.
这是自定义 Flask 错误页面的文档。
Also, take a look at Flask-Bcryptfor password hashing.
另外,看看Flask-Bcrypt的密码散列。
Your CURL command line isn't valid. JSON objects need to have double quotes around the keys and values:
您的 CURL 命令行无效。JSON 对象的键和值需要有双引号:
$ curl -d '{"uuid": "admin", "password": "admin"}' http://127.0.0.1:5000/login/
Now, you can access the keys with request.json.
现在,您可以使用request.json.

