我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'
当我测试这个使用curl
,该GET
调用看起来像
⮀ ~PYTHONPATH ⮀ ⭠ 43± ⮀ curl http://127.0.0.1:5000/login/
GET on login not supported
但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>
虽然在服务器上,我的调试信息打印以下内容
ImmutableMultiDict([("{'uuid': 'admin', 'password': 'admin'}", u'')])
我在哪里做print request.form
。我无法理解,我做错了
谢谢@Blender,但问题是,为什么我不能够访问'uuid'当我发送数据形式的客户端? – daydreamer 2013-04-06 21:10:09
@daydreamer:只有当您访问['request.form'对象]中不存在的键时,Flask才会发送该响应(http://flask.pocoo.org/docs/quickstart/#the-request-object )。尝试使用'request.form.get('uuid',None)'。 – Blender 2013-04-06 21:17:29
正如我的问题所述,我的'request.form'是'ImmutableMultiDict([(“{'uuid':'admin','password':'admin'}”,'')))' – daydreamer 2013-04-06 21:18:56