ScipioAfricanus
ScipioAfricanus

Reputation: 1757

Is it possible to pass a path to a fastapi end point?

I am trying to create a folder traversing api with fastapi. Say I have an end point like this:

@root_router.get("/path/{path}")
def take_path(path):
    logger.info("test %s", path)
    return path

If I do to the browser and call "URL:PORT/path/path"

it returns "path", easy. But if I try "URL:PORT/path/path/path" the code doesnt even get to the logger. I guess that makes sense as the API doesnt have that end point in existence. But it DOES exist on my server. I have figured out other ways to do this, i.e. pass the path as an array of params and rebuld in code with / separator, but passing params in url feels a bit clunky, if I can move through the paths in the url the same as my server, that would be ideal. Is this doable?

Thanks.

Upvotes: 3

Views: 1539

Answers (1)

FireAphis
FireAphis

Reputation: 6780

Add :path to your parameter:

@root_router.get("/path/{path:path}")
async def take_path(path: str):
    logger.info("test %s", path)
    return path

Notice that this is a Starlette feature.

Upvotes: 5

Related Questions