Fastapi: Routed paths must always start with /

Created on 31 Jul 2019  Â·  6Comments  Â·  Source: tiangolo/fastapi

Hi

Let's say I have the following API:

GET /accounts  - list of accounts
POST /accounts  - create account
GET /accounts/{id} - get account by id
DELETE  /accounts/{id} - delete account

then I would make a module accounts with APIRouter

in main.py

app.include_router(accounts.router, prefix="/accounts")

and in accounts.py

router = APIRouter()

@router.get("")
def list_accounts():
    ...

@router.post("")
def list_accounts():
    ...

@router.get("/{id}")
def get_account(id:str):
    ...

but I'm getting an error "Routed paths must always start with /"

if I add to router "/" - then my resources will look like GET /accounts/ , POST /accounts/ (but I do not want slash at the end...)

Basically the idea is to put 5-10 methods inside module and all should start with /accounts

how should i solve this ?

question

Most helpful comment

Thank you, everyone, for the discussion here!

Thanks @vitalik for the PR! More comments on the PR itself: https://github.com/tiangolo/fastapi/pull/415#issuecomment-524591290

You can now use this in FastAPI 0.36.0 with an empty path (""). :tada: :rocket:


I'll close this now, but feel free to add more comments or create new issues.

All 6 comments

You can add a prefix to your router

Le mer. 31 juil. 2019 à 9:44 PM, Vitaliy Kucheryaviy <
[email protected]> a écrit :

Hi

Let's say I have the following API:

GET /accounts - list of accounts
POST /accounts - create account
GET /accounts/{id} - get account by id
DELETE /accounts/{id} - delete account

then I would make a module accounts with APIRouter

in main.py

app.include_router(accounts.router, prefix="/accounts")

and in accounts.py

router = APIRouter()

@router.get("")
def list_accounts():
...

@router.post("")
def list_accounts():
...

@router.get("/{id}")
def get_account(id:str):
...

but I'm getting an error "Routed paths must always start with /"

if I add to router "/" - then my resources will look like GET /accounts/
, POST /accounts/ (but I do not want slash at the end...)

Basically the idea is to put 5-10 methods inside module and all should
start with /accounts

how should i solve this ?

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/tiangolo/fastapi/issues/414?email_source=notifications&email_token=AAINSPS7JB2CYHKM5GDUYSTQCH2T7A5CNFSM4IIKM6LKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HCVH6GQ,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAINSPQJNHLWMDQZPUYQUCTQCH2T7ANCNFSM4IIKM6LA
.

@euri10 yeah, I did - my prefix="/accounts" - but i cannot make api resource /accounts (without ending slash) now with routers

I think you'd have to do:

@router.get("/accounts")
def list_accounts():
    ...

@router.post("/accounts")
def list_accounts():
    ...

@router.get("/accounts/{id}")
def get_account(id:str):
    ...

# ######

app.include_router(accounts.router, prefix="")  # can drop the prefix kwarg

You can mount multiple routers with the same prefix, so this won't cause a problem if you want to use a different router for each resource.

You can make it a little more DRY-friendly via:

prefix = "/accounts"

@router.get(prefix)
def list_accounts():
    ...

@router.post(prefix)
def list_accounts():
    ...

@router.get(prefix + "/{id}")
def get_account(id:str):
    ...

# ######

app.include_router(accounts.router, prefix="")  # can drop the prefix kwarg

You'd have to make sure you didn't create conflicts by naming routers the same thing, but that's always a risk anyway.

(I'm not sure whether there is a really good reason for disallowing "" as the routing path, or if that is more of a leaky implementation detail.)

but what's the technical reason not to allow empty router if parent-include have prefix ?

It does seem sort of awkward that using prefix prevents 'trailing slash' naked endpoints, as OP mentioned.

Good: /cats
Bad: /cats/

(subjectively speaking)

Thank you, everyone, for the discussion here!

Thanks @vitalik for the PR! More comments on the PR itself: https://github.com/tiangolo/fastapi/pull/415#issuecomment-524591290

You can now use this in FastAPI 0.36.0 with an empty path (""). :tada: :rocket:


I'll close this now, but feel free to add more comments or create new issues.

Was this page helpful?
0 / 5 - 0 ratings