question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:28 (5 by maintainers)

github_iconTop GitHub Comments

14reactions
Udayaprasadcommented, Jul 27, 2021

@euri10 @tiangolo

Looks like this bug again comes in the 0.67.0 - latest version. Could you please check once?

APIRouter prefix is not getting utilized in @router.websocket('/') endpoint definition. I am getting 403 error.

router = APIRouter(
    prefix="/substructures",
    tags=["Substructures"],
    responses={404: {"description": "Not found"}},
)

@router.websocket('/')
async def websocket_execute(websocket: WebSocket):
    ....

image

9reactions
tiangolocommented, Apr 17, 2022

Hey all! Some of you might have been experiencing a similar issue related to APIRouters with prefix. That was reported here: https://github.com/tiangolo/fastapi/issues/2639

It was fixed by @Kludex here: https://github.com/tiangolo/fastapi/pull/2640

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

tiangolo/fastapi - Gitter
router = APIRouter() @router.websocket("/") async def ... (just want to make sure it's not due to a difference in the code involved).
Read more >
WebSockets - FastAPI
FastAPI framework, high performance, easy to learn, fast to code, ready for production.
Read more >
How to use FastAPI Depends for endpoint/route in separate file?
Is there a way to make it work? Plus, what is even the purpose of this mechanism in FastAPI? Cannot we just use...
Read more >
Getting Started With FastAPI And More
Basic WebSockets. First, let's make a WebSocket handler in our API. resources/sockets.py. from fastapi import APIRouter, WebSocket, ...
Read more >
Developing a Single Page App with FastAPI and Vue.js
FastAPI Setup; Vue Setup; Models and Migrations; CRUD Actions; JWT Authentication; Routing; Vuex; Components, Views, and Routes.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found