Postman使用Flask Python注册API的问题



我有一个简单的FLASK-API,它与身份验证有关,我的意思是简单的登录,注册页面。对于后端测试,我使用Postman,数据库也使用MySQL。当我从邮差那里输入注册信息时,我收到了一条空消息。我将在下面放弃我的注册路线。

app.route('/register', methods=['GET', 'POST'])
def register():
message = ''
if request.method == 'POST' and 'username' in request.form and 'password' in request.form and 'email' in request.form:
username = request.form['username']
password = request.form['password'].encode('utf-8')
hash_password = hashlib.md5(password).hexdigest()
email = request.form['email']
# Check if user exists using MySQL
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM accounts WHERE username = % s', (username, ))
cursor_email = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor_email.execute( 'SELECT * FROM accounts WHERE email = % s', (email, ))
# Fetch one record and return result
user = cursor.fetchone()
print(type(user))
# If account exists in accounts table in our database

if user:
message = 'This username already exists!'
elif cursor_email.fetchone():
message = 'E-mail already in usage'
elif not email or not username:
message = 'Please fiil out the form'
else:
cursor.execute('INSERT INTO accounts VALUES (NULL, % s, % s, % s)', (username, hash_password, email, ))

mysql.connection.commit()     # Yapılan değişikleri kaydetmek ve veritabında uygulamak için gerekli olan commit fonksiyonu. Since mysql is not a auto-commit DB, it shoulde be done manually
message = 'You have successfully registered!'
return jsonify(message=message)

对于邮差页面;

我的HTTP方法是POST,URL是:http://127.0.0.1:5000/register

JSON格式的正文是:

{
"username": "halilcan",
"password": "123",
"email": "halilcan@gmail.com"

}

在邮差控制台屏幕的最后,我收到了这样的信息;

{
"message": ""
}

可能,我的消息变量最初保持为创建时的状态。

由于字符串是不可变的,因此无法更新值。您只需要删除第一个字符串赋值。

message = ''

您可以在if块中添加一个return语句,如果不满足第一个if条件,则发送一条错误消息。

app.route('/register', methods=['GET', 'POST'])
def register():

if request.method == 'POST' and 'username' in request.form and 'password' in request.form and 'email' in request.form:
username = request.form['username']
password = request.form['password'].encode('utf-8')
hash_password = hashlib.md5(password).hexdigest()
email = request.form['email']
# Check if user exists using MySQL
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM accounts WHERE username = % s', (username, ))
cursor_email = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor_email.execute( 'SELECT * FROM accounts WHERE email = % s', (email, ))
# Fetch one record and return result
user = cursor.fetchone()
print(type(user))
# If account exists in accounts table in our database

if user:
message = 'This username already exists!'
elif cursor_email.fetchone():
message = 'E-mail already in usage'
elif not email or not username:
message = 'Please fiil out the form'
else:
cursor.execute('INSERT INTO accounts VALUES (NULL, % s, % s, % s)', (username, hash_password, email, ))

mysql.connection.commit()     # Yapılan değişikleri kaydetmek ve veritabında uygulamak için gerekli olan commit fonksiyonu. Since mysql is not a auto-commit DB, it shoulde be done manually
message = 'You have successfully registered!'
return jsonify(message=message)

return jsonify(message="some error message")

最新更新