Search code examples
pythonflaskwerkzeug

Retrieving werkzeug route variable args inside a before_request?


If you have variable arguments in your routes, like the "bar" here:

@app.route('/foo/<bar>')
def foo(self, bar):
    return bar

How can you retrieve that in a before_request, like here:

@app.before_request
def before_request():
    app.logger.info(???)

Solution

  • You can use request.view_args to get the path variables.

    Also, you can replace @app.before_request with @app.url_value_preprocessor, like:

    @app.url_value_preprocessor
    def br(endpoint, values):
        print request.path
        print values
    

    Hence, you can get path variables from values and get url path from request.path.