I have this code:
sD = await db.session.getChatbotDetails(request.json.get("apiKey"))
if sD:
setSessionDetails(sD.get_json(), request.json.get("apiKey"))```
and then:
def setSessionDetails(sD, api_key):
session["company_name"] = sD["company_name"]
session["api_key"] = api_key
session["website_url"] = sD["website_url"]
session["welcome_prompt"] = sD["welcome_prompt"]
but then when I log for example
@app.route("/chat", methods=["POST"])
async def chat():
print(session)
if isAuth(request.json.get("apiKey"), "https://website_url") == 200:
but session
returns <SecureCookieSession {}>
... so why isn't it storing the session values.
One thing to be aware of when using session
is that Flask stores that information using cookies and as it says in the documentation:
A note on cookie-based sessions: Flask will take the values you put into the session object and serialize them into a cookie. If you are finding some values do not persist across requests, cookies are indeed enabled, and you are not getting a clear error message, check the size of the cookie in your page responses compared to the size supported by web browsers.
I doubt this is the case if you are only storing that information in session. But worth checking if you are storing a lot of information.
Another possibility is that, you didn't mention how are you making requests to Flask. Like I said in the previous point, session uses cookie to store information and read from it. If you aren't persisting cookies between requests then session
is going to be empty as you are seeing.
For example lets say you have this app.py
from flask import Flask, session
app = Flask(__name__)
app.secret_key = b"A SECRET"
@app.route("/")
def index():
session["company_name"] = "A company name"
session["api_key"] = "An API key"
session["website_url"] = "https://example.com/business"
session["welcome_prompt"] = "Welcome!"
return "Stored cookies"
@app.route("/cookies")
def read_cookies():
return str(session)
if __name__ == "__main__":
app.run(debug=True)
If we use curl to query Flask without storing cookies between requests, when calling /cookies
endpoint then it should be empty.
$ curl http://localhost:5000/
Stored cookies
$ curl http://localhost:5000/cookies
<SecureCookieSession {}>
Now we should try to store cookies and read cookies between requests.
# Store cookies in a file named 'flask-cookies'
$ curl -c flask-cookies http://localhost:5000/
Stored cookies
$ curl -b flask-cookies http://localhost:5000/cookies
<SecureCookieSession {'api_key': 'An API key', 'company_name': 'A company name',
'website_url': 'https://example.com/business', 'welcome_prompt': 'Welcome!'}>