Skip to content

no-slash-collection-root-under-prefix

A collection-root route under a literal router prefix uses an empty path instead of a slash.

Why

With APIRouter(prefix="/items"), a "/" route serves /items/, so a caller of /items gets a 307 redirect. Some API clients do not follow it, or replay it without the body or authorization header.

Fix

Register the collection root with path "" so /items is served directly.

Examples

Before — flagged The collection root is registered as a slash
api.py
from fastapi import APIRouter
router = APIRouter(prefix="/tasks")
@router.get("/")
async def list_tasks() -> list[str]:
return []
After — preferred The collection root is registered as an empty path
api.py
from fastapi import APIRouter
router = APIRouter(prefix="/tasks")
@router.get("")
async def list_tasks() -> list[str]:
return []