Answer the question
In order to leave comments, you need to log in
How to work with cookies in aiohttp/websocket-server?
How to receive and send cookies inside websocket_handler? I tried to use aiohttp_session, but the example given there works with a regular handler, but the same example does not work with websocket_handler: cookies are not sent to the browser. Maybe I did something wrong. It doesn't matter with or without aiohttp_session, but I need to receive and send cookies inside the websocket_handler, tell me how to do it correctly. Is it possible to work with cookies without sessions in this case?
# куки отправляются
async def handler(request):
session = await get_session(request)
session['test'] = 'test'
return web.Response()
# куки НЕ отправляются
async def websocket_handler(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
session = await get_session(request)
session['test'] = 'test'
...
return ws
Answer the question
In order to leave comments, you need to log in
The cookie is sent in the http response, with websocket this response is sent before the data (stream) is sent, so you can try setting the cookie before the response is sent, i.e. to ws.prepare (but why?)
But usually they do this: set cookies in a regular handler (authorization, etc.), they will arrive in websocket_handler when connected
You can also send a command via a web socket, and set a cookie via js
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question