-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
trailing slash in parameter vs non #1127
Comments
What version of fastapi are you using? As of 0.50.0 the trailing slash should be automatically stripped now that fastapi's using the latest version of starlette. Also for what it's worth, even without that feature you could always do something like this: @app.post("/extract/?")
async def create_extraction(...): since routes can take regexes. |
[RESOLVED] |
Thanks for the help @jmriebold ! 🙇♂️ Thanks for reporting back and closing the issue @mwilson8 👍 |
FYI, @jmriebold I don't think path routes can take regex's anymore like this. Since Starlette version 0.13.5 regex is escaped. See the PR here: encode/starlette#932 and further discussion here: encode/starlette#1010 also... in my testing, Starlette is flexible about the trailing slash (and i think by extension fastapi) so you shouldn't need the trailing |
Yeah, I noticed that too. Appears to be the case that regexes were never intentionally supported, it was just an artifact of how paths were constructed. |
It seems like the best option is to just allow Starlette to do the redirect for you, so long as you are okay with redirects. Note, that curl usually doesn't automatically redirect. And neither does requests for POSTs. So there are some valid reasons to not want your users to have to deal with redirects if you have routes they are using programmatically. If you don't want the redirect, I think the other option is using multiple decorators as suggested in the original question. I wish Starlette didn't perform a redirect but just directly accessed the other route, if it matched it. There's probably a good reason they don't do this, though. |
Yeah I personally thought regexes were an elegant way to do it, but have since switched to an explicit redirect route. |
I think this Starlette release broke a lot of services (including mine), and forces us to choose between ugly @router.get("/block", response_model=pydantic_models.BlockResponse)
@router.get("/block/", response_model=pydantic_models.BlockResponse)
async def blocks(request: Request):
... And ugly def slash_patcher(app):
app.router.redirect_slashes = False
routes = copy.deepcopy(app.router.routes)
for r in routes:
if r.path.endswith("/"):
app.add_route(r.path.rstrip("/"), ...)
else:
app.add_route(r.path + "/", ...) Maybe we can fix it somehow in general way? |
Thanks for sharing your slash patcher. Perhaps a pull request or new issue would get the ball rolling? |
Yea, pretty gross. I also think this is probably easier handled in Starlette |
In our team we decided to patch proxy-server for all our FastAPI applications: it ensures that every request does not contain |
Are there any updates on this? Would it be possible to support regex or multiple route options like in @erhosen suggestion
Without ending up with the I've just encountered the same problem, and either using REGEX or being able to add multiple route options would be really helpful. |
In general, redirects should occur automatically to the endpoint w/ or w/o the trailing slash (depending on which one you have configured. If it's absolutely needed to have both routes, you can specify certain routes to not be included in the docs with This gets asked frequently. See this issue for some additional solutions: #2060 |
You should use trailing slash, because there is a caveat, if you have other endpoints with same path. For example, you have endpoints:
On calls for balance, |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
First check
Description
Is there a way to have FastAPI treat a parameter the same if it does or does not have a trailing
/
?Additional context
I want to replicate this in a more concise manner and/or configure FastAPI so I don't have to do this for each path similarly
EDIT: simplified example code
The text was updated successfully, but these errors were encountered: