pythonpython-requestsfalconframeworkfalcon

How can we get path params in falcon middleware, if any path param in the route?


My routes are as follows:

app.add_route('/v1/my_route', MyResource())
app.add_route('/v1/my_route/{app_id}', MyResource())
app.add_route('/v1/my_route2/any_route', AnyRouteResource())
app.add_route('/v1/my_route2/any_route/{app_id}', AnyRouteResource())

and Middleware is something similar to

class MyMiddleware(object):
    def process_request(self, req, resp):
        /** Here i want to get <app_id> value if it is passed **/

Solution

  • You can get every attribute of the request object from req. For example, to get the path of your resource:

    class MyMiddleware(object):
        def process_request(self, req, resp):
            path = req.path
    
            # process your path here
    

    Check the docummentation for more info about requests.

    If you want to get the app_id directly, just extend the method with params, falcon will do the job.

    class MyMiddleware(object):
            def process_request(self, req, resp, params):
                app_id = params["app_id"]