如何返回JSON请求输入并在另一个函数中使用它?

时间:2022-10-24 12:55:09

I'm trying to set up an API login of sorts that when the login is successful, it returns an JWT and first/last name sent with the login from the POST request. My problem is that I cant figure out a way that works to return the first/last name variables to another Class and function. This my code:

我正在尝试设置一个类别的API登录,当登录成功时,它会返回一个JWT以及从POST请求登录时发送的名/姓。我的问题是我无法弄清楚一种方法可以将名字/姓氏变量返回到另一个类和函数。这是我的代码:

@app.route('/login', methods=['POST'])
def login():
    if not request.is_json:
        return jsonify({"msg": "Missing JSON in request"}), 400

    username = request.json.get('username', None)
    password = request.json.get('password', None)
    client_fname = request.json.get('Client First Name', None)
    client_lname = request.json.get('Client Last Name', None)
    if not username:
        return jsonify({"msg": "Missing username parameter"}), 400
    if not password:
        return jsonify({"msg": "Missing password parameter"}), 400

    if username != USER_DATA.get(username) and password not in USER_DATA[username]:
        return jsonify({"msg": "Bad username or password"}), 401

    access_token = create_access_token(identity=username)
    return jsonify(access_token=access_token), 200, PrivateResource.sendData(self, client_fname, client_lname)

class PrivateResource(Resource):
    @app.route('/protected', methods=['GET'])
    @jwt_required

    def sendData(self, client_fname, client_lname):
        return mysqldb.addUser("{}".format(client_fname),"{}".format(client_lname))

I want to return client_fname and client_lname so I can then use them with sendData(). How can I achieve this without have issues with unicode from json or passing the variables?

我想返回client_fname和client_lname,然后我可以将它们与sendData()一起使用。如何在没有来自json的unicode或传递变量的问题的情况下实现这一目标?

2 个解决方案

#1


0  

I think what you need is a Session then you can do something like this-

我认为你需要的是一个会议然后你可以做这样的事情 -

@app.route('/login', methods=['GET', 'POST'])
def login():
   if request.method == 'POST']
          session['username'] = request.form['user']
          return redirect(url_for('home'))

And in route for home use-

在家庭使用的路线 -

@app.route('/home')
def home():
    if 'username' in session:
        return render_template("index.html", name=session['username'])

Hope this helps

希望这可以帮助

#2


0  

You could use session to store the variables you need to access across functions. Add these to login,

您可以使用session来存储跨函数访问所需的变量。将这些添加到登录,

session['client_fname'] = request.json.get('Client First Name', None)
session['client_lname'] = request.json.get('Client Last Name', None)

And access it from sendData as:

并从sendData访问它:

return mysqldb.addUser("{}".format(session['client_fname']),"{}".format(session['client_lname']))

Hope this answers your question.

希望这能回答你的问题。

#1


0  

I think what you need is a Session then you can do something like this-

我认为你需要的是一个会议然后你可以做这样的事情 -

@app.route('/login', methods=['GET', 'POST'])
def login():
   if request.method == 'POST']
          session['username'] = request.form['user']
          return redirect(url_for('home'))

And in route for home use-

在家庭使用的路线 -

@app.route('/home')
def home():
    if 'username' in session:
        return render_template("index.html", name=session['username'])

Hope this helps

希望这可以帮助

#2


0  

You could use session to store the variables you need to access across functions. Add these to login,

您可以使用session来存储跨函数访问所需的变量。将这些添加到登录,

session['client_fname'] = request.json.get('Client First Name', None)
session['client_lname'] = request.json.get('Client Last Name', None)

And access it from sendData as:

并从sendData访问它:

return mysqldb.addUser("{}".format(session['client_fname']),"{}".format(session['client_lname']))

Hope this answers your question.

希望这能回答你的问题。