Fastapi: [BUG] Websocket Routes Only Work on FastAPI, not APIRouter

Created on 24 Mar 2019  路  3Comments  路  Source: tiangolo/fastapi

Describe the bug
Websocket routes appear to only work on the main FastAPI object, not on APIRouter objects. When the same function is copied from a FastAPI object to an APIRouter object instead of working properly it just throws a 403.

To Reproduce
Steps to reproduce the behavior:

  1. The following works as expected:
from fastapi import FastAPI
app = FastAPI()

@app.websocket_route("/hello")
async def hello(websocket):
    await websocket.accept()
    await websocket.send_text("Hello!")
    response = await websocket.receive_text()
    print(response)
    await websocket.close()
    print("Closed")
  1. Moving hello to an APIRouter fails:
# main.py
from fastapi import FastAPI
import other
app = FastAPI()
app.include_router(other.router)
# other.py
from fastapi import APIRouter
router = APIRouter()

@router.websocket_route("/routerhello")
async def hello(websocket):
    await websocket.accept()
    await websocket.send_text("Router Hello!")
    response = await websocket.receive_text()
    print(response)
    await websocket.close()
    print("Router Closed")

Expected behavior
I expect a websocket route to work on both a FastAPI and APIRouter object.

Screenshots
Not applicable.

Environment:

  • OS: macOS 10.14.3
  • FastAPI Version: 0.9.0
  • Python version, get it with: 3.7.2

Additional context
Testing websocket client side with websocat.

bug

Most helpful comment

Thanks for the report!

It was fixed by @euri10 in PR https://github.com/tiangolo/fastapi/pull/100.

It is available in FastAPI verson 0.10.0. :tada: :rocket:

All 3 comments

Thanks for the report!

It was fixed by @euri10 in PR https://github.com/tiangolo/fastapi/pull/100.

It is available in FastAPI verson 0.10.0. :tada: :rocket:

Just tested it out! I'm impressed at how quickly that was fixed, thanks!

Awesome! Kudos to @euri10 for his work 馃榾馃嵃馃尞

Thanks @iwoloschin for reporting back and closing the issue.

Was this page helpful?
0 / 5 - 0 ratings