fastapi post method call with and without trailing slashes

Viewed 784

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:

  • https://......./method/final_path/
  • https://......./method/final_path

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.

1 Answers

Try using APIRouter to define the route instead of directly using the app. The APIRouter has redirect_slashes set to True by default.

Related