Skip to content

prefix parameter in APIRouter does not work for websocket #2639

@schen96

Description

@schen96
# main.py
from fastapi import FastAPI
import myroute

import myroute
app = FastAPI()
app.include_router(myroute.router)

@app.get("/")
async def root():
    return {"message": "hello world!!"}
# myroute.py
from fastapi import APIRouter

router = APIRouter(prefix="/prefix")

# you'd expect ws://localhost:800/prefix/ws to work but only ws://localhost:800/ws connects
@router.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):

    await websocket.accept()
    await websocket.send_text("hi")
    await websocket.close()

# this works fine if you go to http://localhost:8000/prefix
@router.get("/")
def read_root():
    return {"Hello": "World"}

Description

Hi,

So with APIRouter(), the prefix path does not seem to be prepended to the websocket router path.
Not sure if its by design but if it is I feel like its quite misleading

Environment

  • OS: macOS Catalina 10.15.4

  • FastAPI Version: 0.63.0

  • Python version: 3.8.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions