I was trying to write a fastapi post method as:
@app.post('/method/final_path/', tags=['method/final_path'])
When i do a postman call as
https://......./method/final_path/
I get the expected result, but if the call is changed to https://......./method/final_path
I get 405-method not allowed.
According to FastAPI docs, the trailing slashes shouldn't matter, so ideally
@app.post('/method/final_path/', tags=['method/final_path'])
@app.post('/method/final_path', tags=['method/final_path'])
with postman calls:
all the above 4 combinations should give the same result. Then what am I doing wrong?
Versions:
fastapi-0.63
starlette-0.13.6
Thanks in advance.
Try using APIRouter to define the route instead of directly using the app. The APIRouter has redirect_slashes set to True by default.
This is not actual problem already, but helpful insight here.
FastAPI released feature with global redirect_slashes and its True by default. It will redirect to your rout you defined in the code.
@app.get("/route_slash_example")
http://127.0.0.1:8000/route_slash_example/ will be redireceted to http://127.0.0.1:8000/route_slash_example
@app.get("/route_slash_example/")
http://127.0.0.1:8000/route_slash_example will be redirected to http://127.0.0.1:8000/route_slash_example/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With