[BUG] Websocket Routes Only Work on FastAPI, not APIRouter
See original GitHub issueDescribe 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:
- 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")
- 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:
- Created 4 years ago
- Comments:28 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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 getting403
error.Hey all! Some of you might have been experiencing a similar issue related to
APIRouter
s withprefix
. That was reported here: https://github.com/tiangolo/fastapi/issues/2639It 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. 🚀