当我调用我的RESTful API(使用flask实现)从Electron登录时,它不会维护会话。这是一个例子:
@app.route('/login', methods=['POST'])
def login():
userID = request.form['userID']
password = request.form['password']
#login logic
login_status = True
print (session.get('userID'))
if session.get('userID'):
return jsonify({"status": "already logged in"})
if login_status:
session['userID'] = userID
print (session['userID'])
return jsonify({"status": "success"})
else:
return jsonify({"status": "failure"})
当我在Postman中对登录路线进行两次调用时,它告诉我我已经登录。但是,当我从Electron拨打两次登录路线时,它会返回“成功”两次,而不是告诉我已经登录了。知道为什么会这样吗?当从Electron进行调用时,Flask不会保留状态吗?
我在电子中从node.js调用API:
var formData = {
userID: String(user_id),
password: String(password_input)
};
request.post({
url: url,
form: formData,
}, function optionalCallback(err, httpResponse, body) {
if (httpResponse.statusCode != 200) {
console.log(user_id);
console.log(password_input);
console.log(ip_address);
console.log(String(ip_address + 'login'));
return console.error('Connection failed. Incorrect credentials. Status code:', httpResponse.statusCode);
}
// API_IP_address = ip_address;
console.log('Connection successful! Server responded with:', JSON.parse(body));
});