Skip to content

Instantly share code, notes, and snippets.

@andfanilo
Created June 10, 2020 07:32
Show Gist options
  • Save andfanilo/8c76ad86901f0e14c058603df15da1b9 to your computer and use it in GitHub Desktop.
Save andfanilo/8c76ad86901f0e14c058603df15da1b9 to your computer and use it in GitHub Desktop.
[Streamlit] Access request HTTP headers

streamlit/streamlit#1083 (comment)

from streamlit.server.Server import Server
import streamlit as st

def get_headers():
    # Hack to get the session object from Streamlit.

    current_server = Server.get_current()
    if hasattr(current_server, '_session_infos'):
        # Streamlit < 0.56
        session_infos = Server.get_current()._session_infos.values()
    else:
        session_infos = Server.get_current()._session_info_by_id.values()

    # Multiple Session Objects?
    for session_info in session_infos:
        headers = session_info.ws.request.headers
        st.write(headers)
#    return headers
@chapmanjacobd
Copy link

from streamlit.script_run_context import get_script_run_ctx
from streamlit.server.server import Server

session_id = get_script_run_ctx().session_id
session_info = Server.get_current()._get_session_info(session_id)
headers = session_info.ws.request.headers

headers = headers._dict

@dectoplate
Copy link

dectoplate commented Apr 26, 2022

version >= 1.8:

from streamlit.scriptrunner.script_run_context import get_script_run_ctx
[...]

@ahtokca
Copy link

ahtokca commented Sep 12, 2022

Well, streamlit Server.get_current is removed at streamlit/streamlit@a823b55#diff-3f73cf34a1595773b3b79ea85c5efd0c208e23beec4f812fa05419f87cff597e

so it is not possible to get HTTP headers any more :(

@andfanilo
Copy link
Author

andfanilo commented Sep 12, 2022

Hey @ahtokca

Yeah this is currently in discussion here: streamlit/streamlit#5166 and streamlit/streamlit#1083
You should drop a message there if you have a particular usecase in mind

Have a nice day!

@ahtokca
Copy link

ahtokca commented Sep 12, 2022

Thank a lit, @andfanilo

@andfanilo
Copy link
Author

You should now use the dedicated _get_websocket_headers() streamlit/streamlit#5457

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment