Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用flask访问表单数据?_Python_Flask_Flask Extensions_Flask Login - Fatal编程技术网

Python 如何使用flask访问表单数据?

Python 如何使用flask访问表单数据?,python,flask,flask-extensions,flask-login,Python,Flask,Flask Extensions,Flask 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(re

我的
登录
端点看起来像

@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'
⮀ ~PYTHONPATH ⮀ ⭠ 43± ⮀ curl http://127.0.0.1:5000/login/
GET on login not supported
当我使用
curl
测试时,
GET
调用如下

@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'
⮀ ~PYTHONPATH ⮀ ⭠ 43± ⮀ curl http://127.0.0.1:5000/login/
GET on login not supported
但是在
POST
上,我无法访问表单数据并获取
HTTP400

⮀ ~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>

我在哪里执行
打印请求。表单
。我无法理解我哪里做错了

您仍然需要返回响应:

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
这是您的文档

另外,看看密码散列


您的CURL命令行无效。JSON对象需要在键和值周围使用双引号:

$ curl -d '{"uuid": "admin", "password": "admin"}' http://127.0.0.1:5000/login/

现在,您可以使用
request.json
访问键,因为您没有正确使用
curl
。试着这样做:

curl -d 'uuid=admin&password=admin'
curl -d '{"uuid":"admin","password":"admin"}' -H "Content-Type: application/json" 
当您试图从
Request.form
获取不存在的密钥时,
400错误请求是常见的行为

或者,使用
request.json
而不是
request.form
并像这样调用
curl

curl -d 'uuid=admin&password=admin'
curl -d '{"uuid":"admin","password":"admin"}' -H "Content-Type: application/json" 

谢谢@Blender,但问题是为什么我在发送数据表单客户端时无法访问
uuid
?@daydreamer:Flask仅在您访问客户端中不存在的密钥时才会发送该响应。试着使用
request.form.get('uuid',None)
。正如我在问题中提到的,我的
request.form
ImmutableMultiDict([(“{'uuid':'admin','password':'admin'},u')))
@daydreamer:我知道。你是如何发送这个请求的?像这样
curl-xpost-d'{uuid:“admin”,“password:“admin”}http://127.0.0.1:5000/login/