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

2.7k Views Asked by At

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 **/
2

There are 2 best solutions below

4
On BEST ANSWER

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"]
0
On

There is process_resource(self, req, resp, resource, params) method in the base middleware. You can override it. There params is a dict like object with params extracted from the uri template fields.

https://falcon.readthedocs.io/en/stable/api/middleware.html