简体   繁体   中英

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

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

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

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