pyropy / fastapi-socketio

Easily integrate socket.io with your FastAPI app 🚀
Apache License 2.0
328 stars 31 forks source link

Adding CORS middleware and using SocketManager causes multiple values for Access-Control-Allow-Origin header #23

Closed deanthing closed 2 years ago

deanthing commented 2 years ago

After adding both CORSMiddleware from FastApi and using SocketManager, which adds another CORS head, it results in the following error when trying to connect from the client:

The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:4200, http://localhost:4200', but only one is allowed.

This is my fast api set up code:

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=['http://localhost:4200'],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

socket_manager = SocketManager(app=app, async_mode="asgi", cors_allowed_origins="*")

The first CORS middleware access is so that the client can query the API but then the socketmanager also adds its own header.

Any solutions / help would be much appreciated!

deanthing commented 2 years ago

As per a previous comment, setting cors_allowed_origins=[] in the SocketManager constructor fixes this issue.