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 is set:
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
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 is set:
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
Share
Improve this question
edited Feb 16 at 17:15
Chris
34.3k10 gold badges99 silver badges234 bronze badges
asked Feb 16 at 4:18
ImisioluwaImisioluwa
13 bronze badges
0
1 Answer
Reset to default 0As described in this answer, you would need to use Session
objects in Python requests
library, or a Client
instance from the httpx
library, in order to persist cookies across requests.
Example (using requests
)
import requests
s = requests.Session()
s.get('https://httpbin./cookies/set/sessioncookie/123456789')
r = s.get('https://httpbin./cookies')
print(r.text)
# '{"cookies": {"sessioncookie": "123456789"}}'
s.close()
You might find this answer helpful as well.