question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] trailing slash in parameter vs non

See original GitHub issue

First 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:closed
  • Created 4 years ago
  • Reactions:8
  • Comments:15 (9 by maintainers)

github_iconTop GitHub Comments

8reactions
jmrieboldcommented, Mar 17, 2020

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.

5reactions
falkbencommented, Apr 14, 2021

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found