Access Request Header in Streamlit

Is there any way to access incoming header data in the streamlit site? I think in a flask site, you’d be handling the incoming request, could parse its header, and use that data in your site’s response. I’m wondering if all that data is unavailable to my site because streamlit/tornado(?)/requests is doing the request handling behind the scenes.

I have a streamlit site sitting behind an nginx reverse proxy. For the core of my question I’m not sure if the nginx layer matters but here’s a little info about how I’m using it. I have nginx adding authentication through its basic auth module. My goal is to find out the authenticated user’s username within my site. I believe nginx can forward the username as a field in the header (proxy_set_header REMOTE_USER $remote_user) but from what I can see, streamlit isn’t going to expose a way to read this REMOTE_USER field from the header.

1 Like

Hi @agray,

Yes, your intuition is correct – Streamlit is doing a lot as middleware to obscure connections, so you can’t really crack them open and use that information the same way you would in a Flask route.

The way Streamlit is put together, the javascript (Node) end of the system handles user interface generation and responds to user input, and it communicates with Python (Tornado) over Websockets. This means that on the server side, unless the front-end has specifically collected and resent the information from a request over a websocket, your script will not have access to the original HTTP request header.

So, I’ve created a feature request about passing user HTTP request headers so they can be seen on the Python side. A feature like this will require a bit of careful thinking to avoid security issues, but it’s not out of the question.

Feel free to comment on the issue if you have any further thoughts. Thanks for the suggestion!

1 Like

Hey there! I noodled on this for a while, and figured out what seems to be a secure implementation. Requires nginx & streamlit. The README at my repo has more details on the specifics of just the user bit if you scroll down