Skip to content

Instantly share code, notes, and snippets.

@skannan-maf
Last active April 27, 2024 01:53
Show Gist options
  • Save skannan-maf/f803cddee9cc555ceb62405124d418a3 to your computer and use it in GitHub Desktop.
Save skannan-maf/f803cddee9cc555ceb62405124d418a3 to your computer and use it in GitHub Desktop.
def get_all_cookies():
'''
WARNING: We use unsupported features of Streamlit
However, this is quite fast and works well with
the latest version of Streamlit (1.27)
RETURNS:
Returns the cookies as a dictionary of kv pairs
'''
from streamlit.web.server.websocket_headers import _get_websocket_headers
# https://github.com/streamlit/streamlit/pull/5457
from urllib.parse import unquote
headers = _get_websocket_headers()
if headers is None:
return {}
if 'Cookie' not in headers:
return {}
cookie_string = headers['Cookie']
# A sample cookie string: "K1=V1; K2=V2; K3=V3"
cookie_kv_pairs = cookie_string.split(';')
cookie_dict = {}
for kv in cookie_kv_pairs:
k_and_v = kv.split('=')
k = k_and_v[0].strip()
v = k_and_v[1].strip()
cookie_dict[k] = unquote(v) #e.g. Convert name%40company.com to name@company.com
return cookie_dict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment