-
Describe the bugWhen a delete is made on a post route with a trailing slash the server returns a 307 temporary redirect. I would either expect a 404 not found or a 405 method not allowed. To ReproduceJust a unittest to show the case: from fastapi import FastAPI, Request, Response
from fastapi.testclient import TestClient
app = FastAPI()
@app.post("/bla")
async def bla():
return {}
client = TestClient(app)
def test_read_main():
response = client.delete("/bla/")
print(response.headers)
assert response.status_code == 405 Environment
|
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 1 reply
-
Your path has some error. |
Beta Was this translation helpful? Give feedback.
-
Interesting. This behavior is actually controlled by However on my end during the TestClient test that property is not kept, which might be a bug: from fastapi import FastAPI, APIRouter
from starlette.testclient import TestClient
app = FastAPI()
router = APIRouter(redirect_slashes=False)
@router.post("/bla")
async def bla():
return {}
app.include_router(router)
client = TestClient(app)
def test_read_main():
assert router.redirect_slashes == False # success
assert client.app.router.redirect_slashes == False #fail
response = client.post("/bla/")
print(response.headers)
assert response.status_code >= 400 @Dustyposa I think they just want that path without slashes not getting redirected. It's intended. |
Beta Was this translation helpful? Give feedback.
-
Yep, this seems to be a bug but I can't figure out where it is coming from. The Starlette is only supposed to be redirecting slashes if Very strange. |
Beta Was this translation helpful? Give feedback.
-
Just had a look into this. The main When you create separate This should work:
Seems that starlette/routing.py:601
Also, if you are using a FastAPI or Flask backend via a proxy (like in CRA |
Beta Was this translation helpful? Give feedback.
-
Now the question should be, what is default behavior and what is HTTP standard? |
Beta Was this translation helpful? Give feedback.
-
I haven't seen anything about what should be redirected and what not, I imagine there's no standard for automatic slash redirects, it's mostly a "feature" of frameworks. In your case, I imagine the best would be to update the test to use the exact path. And if you don't want automatic redirects, I guess you could disable them and that would make things more strict. But anyway, were you able to solve your problem? If so, you could close this issue. 🤓
|
Beta Was this translation helpful? Give feedback.
-
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs. |
Beta Was this translation helpful? Give feedback.
Just had a look into this. The main
FastAPI
instance has anAPIRouter
.When you create separate
fastapi.routing.APIRouter
instances and then useapp.include_router(...)
, behind the scenes it is actually appending the routes from this router toFastAPI#router
.This should work:
Seems that
redirect_slashes
means "redirect paths without trailing slash to slash", NOT "redirect trailing slash to no slash".starlette/routing.py:601
Also, if y…