简体   繁体   中英

persist local scope variables with before_request hook in python bottle

I would like to use the before_request hook to load the request body into json format so I don't have to do it for each request as I know this is something I will need to have with any api call. Is there a way I can persist the data dict into the view functions.

@app.hook('before_request')
def before():
    data = json.loads(request.data)

@app.route("/start", method='POST')
def start():
   foo = data[bar] #how do i do this??
   ...

Every page call is a separate request context and while you're handling that call, you'll have access to the request data. There's no need for you to duplicate that in a JSON or dictionary structure. If you need to cache app settings or other static values that won't change with each request, you can store them in a config file or global/module variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM