Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using prefix on APIRouter with websockets doesn't work #2634

Closed
danrobinson88 opened this issue Jan 12, 2021 · 3 comments
Closed

Using prefix on APIRouter with websockets doesn't work #2634

danrobinson88 opened this issue Jan 12, 2021 · 3 comments

Comments

@danrobinson88
Copy link

danrobinson88 commented Jan 12, 2021

Hi,

to reproduce:

router = APIRouter(
    prefix="/v1"
)

@router.websocket_route("/calculate")
async def websocket_endpoint(websocket: WebSocket) -> None:
    pass

app = FastAPI()
app.include_router(router)

This doesn't work, meaning calling the server with websocket with path /v1/calculate fail.

Whereas, it does work without prefix:

router = APIRouter()

@router.websocket_route("/v1/calculate")
async def websocket_endpoint(websocket: WebSocket) -> None:
    pass

app = FastAPI()
app.include_router(router)

fastapi version: 0.63.0

Fixing this would be highly appreciated :)

@danrobinson88 danrobinson88 added the question Question or problem label Jan 12, 2021
@danrobinson88 danrobinson88 changed the title Using prefix on APIRouter with websockets doesn't work [Bug] Using prefix on APIRouter with websockets doesn't work Jan 12, 2021
@danrobinson88 danrobinson88 changed the title [Bug] Using prefix on APIRouter with websockets doesn't work [BUG] Using prefix on APIRouter with websockets doesn't work Jan 12, 2021
@Kludex
Copy link
Sponsor Collaborator

Kludex commented Jan 12, 2021

Solution implemented on: #2640

marcost2 added a commit to Espolvoritas/BackEnd that referenced this issue Oct 19, 2021
* As seen in tiangolo/fastapi#98 and tiangolo/fastapi#2634 websocket does not currently inherit the route prefix correctly, as such patch it to keep the websocket consistent with the rest of the file
* Also move userID to path parameters since websockets dont support body parameters
marcost2 added a commit to Espolvoritas/BackEnd that referenced this issue Oct 23, 2021
* As seen in tiangolo/fastapi#98 and tiangolo/fastapi#2634 websocket does not currently inherit the route prefix correctly, as such patch it to keep the websocket consistent with the rest of the file
* Also move userID to path parameters since websockets dont support body parameters
@tiangolo
Copy link
Owner

Thanks for the help here @Kludex ! 👏 🙇

If that solves the original problem, then you can close this issue @danrobinson88 ✔️

Sorry for the long delay! 🙈 I wanted to personally address each issue/PR and they piled up through time, but now I'm checking each one in order.

@danrobinson88
Copy link
Author

@tiangolo it does, will do, thanks.

@tiangolo tiangolo changed the title [BUG] Using prefix on APIRouter with websockets doesn't work Using prefix on APIRouter with websockets doesn't work Feb 24, 2023
@tiangolo tiangolo reopened this Feb 27, 2023
Repository owner locked and limited conversation to collaborators Feb 27, 2023
@tiangolo tiangolo converted this issue into discussion #6955 Feb 27, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

3 participants