The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:XXXX, *', but only one is allowed. #1304
-
Stack: FastApi with Uvicorn (Server: Python, Client: JS) Example code:
As allowed origins is specified twice, first in app.add_middleware and second in socketio.AsyncServer, the error occurs. SocketIO works if allow_origins option in app.add_middleware is disabled ('Access-Control-Allow-Origin' header now only contains a single value), but in that case, API endpoints depending on CORS middleware by app.add_middleware will no longer by accessible to fetch requests made by the JS client. Similar issue found here: encode/starlette#1309 (comment) Is anyone able to provide any insights? Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
You have configured CORS twice. First in FastAPI, then again in Socket.IO. Try removing the Socket.IO CORS configuration. If that does not work, then I suggest you adopt the recommended method for mixing an ASGI app with Socket.IO. You can see an example here: https://github.com/miguelgrinberg/python-socketio/blob/main/examples/server/asgi/fastapi-fiddle.py. |
Beta Was this translation helpful? Give feedback.
-
Hi Miguel, Thanks for answering. This is my current code:
but now am faced with the error "http://localhost:XXXX is not an accepted origin." Thanks |
Beta Was this translation helpful? Give feedback.
I probably wasn't very clear. With your initial code, I suggested you remove CORS from Socket.IO, because you had Socket.IO as a route mounted on the FastAPI app. If that didn't work, then use the method I recommend you have to enable CORS on both sides, because the FastAPI and the Socket.IO sides run independently.