简体   繁体   English

中间件在流式响应 flask 应用程序中捕获异常

[英]middleware caught exception in streamed response flask app

This is an simple application that inserts data into a mysql db.这是一个将数据插入 mysql 数据库的简单应用程序。 The data gets inserted correctly but i am not getting the reponse of my post request but the status is 200.request.form works but i want to send json as payload This is my below route and i believe something is wrong when i am checking if the keys exist in my json payload.数据已正确插入,但我没有收到我的发布请求的响应,但状态为 200.request.form 有效,但我想将 json 作为有效负载发送这是我的以下路线,我相信当我检查是否有问题时密钥存在于我的 json 有效负载中。 i tried using request.json,request.get_json,request.get_json() but i am unable to get rid of this error although the status is 200 but application is not returning any message.我尝试使用 request.json,request.get_json,request.get_json() 但我无法摆脱此错误,尽管状态为 200 但应用程序未返回任何消息。

@app.route('/pythonlogin/register', methods=['GET', 'POST'])
def register():
    # Output message if something goes wrong..
    msg = ''
    # current_time = time.strftime('%Y-%m-%d %H:%M:%S')
    # Check if "username", "password" and "email" POST requests exist (user submitted form)
    if request.method == 'POST' and request.get_json().get('name') != None and  request.get_json().get('password')  != None and  request.get_json().get('email')  != None and  request.get_json().get('size')!= None  :
        data = request.get_json().get
        # Create variables for easy access
        name = data('name')
        password = data('password')
        email = data('email')
        size = data('size')
        # Check if account exists using MySQL
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute('SELECT * FROM user WHERE email = %s', (email,))
        account = cursor.fetchone()
        print(account)
        # If account exists show error and validation checks
        if account:
            msg = 'Account already exists!'
        elif not re.match(r'[^@]+@[^@]+\.[^@]+', email):
            msg = 'Invalid email address!'
        elif not re.match(r'[A-Za-z0-9]+', name):
            msg = 'Username must contain only characters and numbers!'
        elif not re.match(r'[0-9]',size):
            msg = 'Team size accepts only numbers'
        elif not name or not password or not email or not size:
            msg = 'Please fill out the form!'
        else:
            print('inside else')
            role = 1
            # Account doesnt exists and the form data is valid, now insert new account into accounts table
            cursor.execute('INSERT INTO company VALUES (NULL, %s,NULL, %s, %s)', (name, size,current_time))
            #cursor.execute('INSERT INTO user VALUES (NULL, %s, %s, NULL,NULL, NULL,NULL, NULL,NULL, NULL)', (email, password,))
            mysql.connection.commit()
            cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
            cursor.execute('SELECT * FROM company WHERE name = %s ' ,(name,))
            results = cursor.fetchone()
            comp_id = results['company_id']
            cursor.execute('INSERT INTO user VALUES (NULL, NULL, %s,%s, %s,NULL, NULL,NULL, %s,%s)', (email, password,role,current_time,comp_id,))
            mysql.connection.commit()
            msg = 'You have successfully registered!'
    else :
        # Form is empty... (no POST data)
        msg = 'Please fill out the form!'
    return {"status":"1","message":msg}

this is my below response:这是我的以下回复:

Debugging middleware caught exception in streamed response at a point where response headers were already sent 

and this和这个

MySQLdb._exceptions.OperationalError: (2006, '').

Eventually i declared this twice最终我宣布了两次

mysql = MySQL(app)

Incase if you are using Flask-migrate, unapplied migrations also cause the same kind of exceptions.如果您使用 Flask-migrate,未应用的迁移也会导致相同类型的异常。 make sure you follow the below steps properly确保您正确遵循以下步骤

flask db migrate

then if you skip the below step (un-apply the model changes to db).然后如果您跳过以下步骤(取消应用 model 更改为 db)。 this will cause the same kind of exceptions.这将导致相同类型的异常。 run the flask db upgrade command soon after using the flask db migrate command使用flask db migrate命令后立即运行flask db upgrade命令

flask db upgrade

then start the flask server and start hitting your apis.然后启动 flask 服务器并开始访问您的 api。

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

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