[QUESTION] trailing slash in parameter vs non
See original GitHub issueFirst check
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
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
@app.post("/extract")
@app.post("/extract/")
async def create_extraction(...):
EDIT: simplified example code
Issue Analytics
- State:
- Created 4 years ago
- Reactions:8
- Comments:15 (9 by maintainers)
Top Results From Across the Web
Is a trailing slash appropriate before URL parameters?
Both versions of your URL with parameters are "legal". The trailing slash can be there if you choose, or you can omit it....
Read more >To slash or not to slash | Google Search Central Blog
Choose one URL as the preferred version. If your site has a directory structure, it's more conventional to use a trailing slash with...
Read more >Trailing slash before a query string. Bad practice?
It's valid: The path component may end with a slash ( / ). The query component starts with the first question mark (...
Read more >Trailing slash included before query parameters
A query parameter (or a list of them) follows after the entire endpoint separated from it by a question mark ("?") and from...
Read more >Pagination trailing slash
In my projects I always remove this trailing slash and all is fine. ... @bestmomo are you referring to a trailing ? with...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub 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:
since routes can take regexes.
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
include_in_schema=False
This gets asked frequently. See this issue for some additional solutions: https://github.com/tiangolo/fastapi/issues/2060