Replies: 1 comment 2 replies
-
|
The way I have always done this is with two handlers: @app.get('/test')
@app.get('/test/<path:path>')
async def test(request, path=''):
passDoesn't this address your issue? |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
In my application I need the remaining path …../example/remaining to be represented as a List.
For the root I need and expect an empty list.
However, using <path:path> handler, an empty path is not triggered for the root, which excludes handling the root node
So Miguel it would be great to have a look in this.
My recommendation would be, to
- let <path:path> trigger also when remaining path is empty to be able to handle root node
- and/or add a pathList handler (see below) to the microdot implementation as it makes really sense in number of implementations
…so see below what i found out
I tried the following with 3 implementations
1) using regular remaining path path:path
Does not work as it did only trigger for b) when remaining path is NOT empty, not for a) when remaing path IS empty
Instead I get "GET /testA/ 404" from microdot
@app.get('/testA/<path:path>')
async def index(request,path):
pathList = list(filter(lambda x: x.strip(), path.strip('/').split('/')))
print(path, " => ", pathList)
return
2) using regular regular expression
It does trigger for a) and b) so I could convert empty string to an empty list
Although I get "GET /testB/ 204" from microdot
@app.get('/testB/<re:[\x20-\x7E]*:path>')
async def index(request,path):
pathList = list(filter(lambda x: x.strip(), path.strip('/').split('/')))
print(path, " => ", pathList)
return
3) register a 'pathList' handler, my preferred solution
It does trigger for a) and b) and the handler returns the path as an empty list
Although I get "GET /testC/ 204" from microdot
@app.get('/testC/<pathList:path>')
async def index(request,path):
print(path)
return
URLPattern.register_type('pathList',
pattern = '[\x20-\x7E]*',
parser=lambda path: list(filter(lambda x: x.strip(), path.strip('/').split('/'))))
Beta Was this translation helpful? Give feedback.
All reactions