Skip to content
Advertisement

Can not get cookie from localhost websocket fast api

I’m testing websockets to work with cookies and trying to get them in fast api. I manually installed them in chrome but I get an empty dictionary inside the application. I used the fast api documentation templates and slightly redesigned it

enter image description here

My html

JavaScript

My websocket

JavaScript

print(websocket.cookies) returns an empty dictionary {}

Advertisement

Answer

Cookies are domain-defined, so you should point at localhost:8000 and there define that Cookie, not 127.0.0.1:8000.

Maybe it’s easier to check them in Postman.

With such crafted request: enter image description here

They will be available at websocket.cookies for sure.

websocket.cookies under the hood checks Cookie header:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement