Skip to content
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

prefix parameter in APIRouter does not work for websocket #2639

Closed
schen96 opened this issue Jan 12, 2021 · 15 comments
Closed

prefix parameter in APIRouter does not work for websocket #2639

schen96 opened this issue Jan 12, 2021 · 15 comments
Labels
answered bug Something isn't working reviewed

Comments

@schen96
Copy link

schen96 commented Jan 12, 2021

# 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

@schen96 schen96 added the question Question or problem label Jan 12, 2021
@Kludex
Copy link
Sponsor Collaborator

Kludex commented Jan 12, 2021

Bug confirmed.

Pushing a PR to fix in some minutes. Thank you for the report! @schen96

@Kludex
Copy link
Sponsor Collaborator

Kludex commented Jan 12, 2021

Done! @tiangolo

@floriandorre
Copy link

Hi,

Thanks for the fix @Kludex, do you know if this PR going to be merged any time soon ?

@Kludex
Copy link
Sponsor Collaborator

Kludex commented May 21, 2021

I have no idea. Let me ping @tiangolo

@ais-one
Copy link

ais-one commented May 29, 2021

Hi I would like to say that this is quite important, as I would like to move code out of the main.py file, and this feature would help me to do so...

@RuellePaul
Copy link

Same here, I would really appreciate this PR to be merged

@Sh4pe
Copy link

Sh4pe commented Jul 6, 2021

I'd also highly appreciate #2640 being merged.

@mh739025250
Copy link

Hope this PR to be merged soon.

@okay1204
Copy link

Still waiting over here as well.

@anton000v
Copy link

Waiting too...

@tiangolo

@kpitzen
Copy link

kpitzen commented Feb 11, 2022

I am also waiting for this! :)

@OkilSaber
Copy link

Hello @tiangolo, I am also waiting for this bug fix. Thank you for your work :)

@E5presso
Copy link

E5presso commented Apr 1, 2022

Hi @tiangolo, Is there any plan to merge this?

@tiangolo tiangolo added bug Something isn't working question Question or problem and removed question Question or problem labels Apr 17, 2022
@tiangolo
Copy link
Owner

Thanks for the report @schen96!

And thanks @Kludex for the quick fix in #2640! Sorry for the delay, there are lots of PRs to review. 😅

The fix will be available in FastAPI version 0.75.2, released in a couple of hours. 🚀

@github-actions
Copy link
Contributor

Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered bug Something isn't working reviewed
Projects
None yet
Development

No branches or pull requests