fastapi-socketio icon indicating copy to clipboard operation
fastapi-socketio copied to clipboard

multiple CORS values in socketio requests error when using CORSMiddleware

Open chinchaun opened this issue 3 years ago • 2 comments

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 Screen Shot 2021-12-30 at 23 13 11

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

chinchaun avatar Dec 31 '21 02:12 chinchaun

Hi @chinchaun ,

I am also facing the same issue but not able to connect after using the same solution. Any insights ?

niteshgaba avatar Apr 16 '22 07:04 niteshgaba

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.

chinchaun avatar Apr 17 '22 18:04 chinchaun