0

I need to use user's setup configuration in a Flask APP. The user will post certain data ( id, age etc) and I have to use this configuration in other methods as well. Now this is what I have tried which is not working

data_config = {}

@app.route('/api/data', methods=['POST'])
def check():
    payload = request.get_json()
    data_config = payload
    print(data_config)
    return jsonify(data_config)

I am getting the data in data_config . But when I make the following call I am getting data empty

@app.route('/api/get_config', methods=['GET'])
def getCofig():
    return jsonify(data_config)

How can I fix this ?

TheTechGuy
  • 1,568
  • 4
  • 18
  • 45

1 Answers1

3

There is global missing. Try this:

data_config = {}

@app.route('/api/data', methods=['POST'])
def check():
    global data_config
    payload = request.get_json()
    data_config = payload
    print(data_config)
    return jsonify(data_config)

BTW your solution looks hacky, maybe you could consider saving data_config into a database?

Kamil
  • 1,256
  • 10
  • 17
  • 1
    This is the answer, but OP, really really don't use `global`. There is 99.9999% of the time a better solution and you're just asking for trouble. – FHTMitchell Aug 22 '18 at 09:36
  • using DB was in consideration. This will solve the problem I was dealing with. Thanks for the help – TheTechGuy Aug 22 '18 at 09:40