fastapi-socketio
fastapi-socketio copied to clipboard
multiple CORS values in socketio requests error when using CORSMiddleware
Hello humans, I faced an issue when I was trying to use fastapi-socketio and the CORSMiddleware
app = FastAPI()
sio = SocketManager(app=app)
# CORS
# Set all CORS enabled origins
if config.BACKEND_CORS_ORIGINS:
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
and this error was showing up
The workaround I found was to set to an empty list the cors_allowed_origins
parameter on the SocketManager to prevent the duplication of the values
Solution
app = FastAPI()
sio = SocketManager(app=app, cors_allowed_origins=[])
# CORS
# Set all CORS enabled origins
if config.BACKEND_CORS_ORIGINS:
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
The solution was taken from here
if I have some time, I will try to fix it, Cheers
Hi @chinchaun ,
I am also facing the same issue but not able to connect after using the same solution. Any insights ?
Hey @niteshgaba, mmm I don't know how to help you, I remember I have to dig into the source code and the issue mentioned above to find the workaround and I just worked.