[Python] Issue with streamlit and FastAPI cookie management

Discussão em 'Python' iniciado por Stack, Fevereiro 16, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to access the token from a request in my FastAPI app that is running in my docker container endpoint but it is failing to get the cookies

    @user.get('/get_cookies')
    async def get_cookies(request: Request, user: user_dependency):
    if not user:
    raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
    access_token = request.cookies.get('access_token')
    refresh_token = request.cookies.get('refresh_token')

    print(f"Access Token: {access_token}, Refresh Token: {refresh_token}")
    if access_token and refresh_token:
    return {'access_token': access_token, 'refresh_token': refresh_token}
    else:
    return "Failed to fetch access token and refresh token"



    I am using requests to make a request to the API from my streamlit app

    import streamlit as st
    import requests

    p_uri = "http://localhost:8000/user/get_cookies"

    response = requests.get(p_uri)
    st.write(response.json())


    this is how the cookie isset


    response = RedirectResponse(url='http://localhost:8501')

    response.set_cookie(
    key='jwt_token',
    value=jwt_token,
    httponly=False,
    max_age=60 * 60 * 24 * 30,
    secure=False,
    samesite='lax',
    domain='localhost',
    path='/'
    )
    response.set_cookie(
    key='access_token',
    value=token,
    max_age=60 * 60 * 24 * 30,
    httponly=False,
    secure=False,
    samesite='lax',
    domain='localhost',
    path='/'
    )

    response.set_cookie(
    key='refresh_token',
    value=refresh_token,
    max_age=60 * 60 * 24 * 30,
    httponly=False,
    secure=False,
    samesite='lax',
    domain='localhost',
    path='/'
    )

    return response

    Continue reading...

Compartilhe esta Página