我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