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

2

7

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 **/
Kaylor answered 17/8, 2018 at 7:46 Comment(0)
M
4

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"]
Mosby answered 22/9, 2018 at 17:43 Comment(4)
I have to parse req.path this way, that I don't want to do, anyways got the answer from @MikhailKaylor
I dont understand what you are saying. Do you mean you whant to parse the Uuid from the path?Mosby
yes, I wanted <app_id> in middleware, which is in path params.Kaylor
Then you just need to add an argument to the middleware the param. I will update the answer.Mosby
U
2

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

Ustkamenogorsk answered 22/9, 2018 at 17:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.