0

I'm trying out Flask, I have this code:

responseDict = {}
responseDict["_hits"] = {}

app = Flask(__name__)

@app.route('/server')
def showMain():
        query = request.args.get('q')
        responseDict["_hits"]["random string here"] = query
       
        return jsonify(responseDict)

Example:

  • I make a simple GET /server?q=earth
  • I make another GET /server?q=moon

I get the correct _hits dictionary with the query I provided, but when I make the GET /server?q=moon I also get the previous query, earth How do I delete everything on responseDict when the request is done?

Edit:

responseDict has to be a global variable because it's used for server logging.

Community
  • 1
  • 1

1 Answers1

0

responseDict is a global variable. This is why all the request will be added. Try this:

app = Flask(__name__)

@app.route('/server')
def showMain():
        query = request.args.get('q')
        responseDict = {}
        responseDict["_hits"] = {}
        responseDict["_hits"]["random string here"] = query

        return jsonify(responseDict)

Edit: As you wanna keep responseDict as global variable. What about copying this and return it?

responseDict = {}
responseDict["_hits"] = {}

app = Flask(__name__)

@app.route('/server')
def showMain():
        query = request.args.get('q')
        temp_responseDict = responseDict
        temp_responseDict["_hits"]["random string here"] = query

        return jsonify(temp_responseDict)
user2874583
  • 537
  • 1
  • 4
  • 13