How to extract headers in streamlit app

We have a portal where we are using streamlit embedded in iframe to display some charts, dashboards etc…, also we have Nginx configured to route this iframe request to streamlit. For authentication & authorisation of streamlit app we decided to use session id(created and stored in cookies when user login to our portal) which is passed through header from our portal frontend. How can I extract this header in streamlit app so that I can authenticate or authorise the requests

I referred few github issues but seems like they are outdated for my streamlit version == 1.13.0

It would be really helpful if some one guides me here

Sorry this is a side question - but can you tell me why you’re embedding? Also are you embedding a Streamlit Cloud deployed app or one you’ve deployed yourself? We’re looking into how to support embedding better, and I’d love to know more about your use case!

We have deployed streamlit app by ourselves.

Our use case is, we have a platform which helps ecommerce store owners in increasing their sales. We have built tables and charts of number of orders, abandoned checkouts etc… that can have dynamic filters. So whenever store owner log in to our portal they can use this tables and charts to know more about their store performance.

So we built this entire analytics part in streamlit and embedded this in iframe on UI

1 Like

Hi @Swastik_K :wave:

We have a new “internal” (i.e.: subject to change without deprecation!) API to access a copy of the headers from the current session’s incoming WebSocket request.:

import streamlit as st
from streamlit.web.server.websocket_headers import _get_websocket_headers

headers = _get_websocket_headers()
access_token = headers.get("X-Access-Token")
if access_token is not None:
  # authenticate the user or whatever
  ...

Please take a look at the following pull request to learn how to update your code:

1 Like

thank you @snehankekre for the help, will try this

1 Like

hey @snehankekre I couldn’t find websocket_headers.py under /streamlit/web/server/ package. I’ve installed streamlit using

pip3 install streamlit==1.13.0

This api not available in 1.13.0 version?.

Is there any other way to get headers?

The fix was created after the 1.13.0 release. You will have to either wait for the next release on the 27th or try to see if the fix is available in streamlit-nightly.

The last option is to try workarounds described in this GitHub issue:

But I would wait for the next release or check streamlit-nightly.

1 Like

Will wait till next release :smiley:. Thank you

1 Like

Is this little hack useful?

1 Like