I am making a chatbot and will be hosting it online and naturally I need to use flask sessions (or something similar) in order to keep chat context different for different users. For some reason, when I just call the API URL in my browser window, the sessions are tracked properly. However when I use the chatbot interface to call the API URL, the server thinks that every request is a new session.
I simplified the question route into just being a counter as shown below:
@app.route('/question/\<q\>')def answer(q): session['counter'] = session.get('counter', 0) + 1 print(session['counter']) session.modified = True return "done"
Here is the javascript code that calls this route:
const generateResponse = (incomingChatLi) => { const API_URL = "http://127.0.0.1:5000/question/" + encodeURIComponent(userMessage); const messageElement = incomingChatLi.querySelector("p"); fetch(API_URL,{ method:'GET', credentials:'include' }).then(response => response.text()).then(data =>{ messageElement.textContent = data; chatbox.scrollTo(0,chatbox.scrollHeight); }).catch(error => { console.error('Error:',error) messageElement.textContent = "Error fetching response"; messageElement.classList.add("error"); });}
Does anyone know why calling the url "http://127.0.0.1:5000/question/hi" for example increases the counter everytime I make a request, but when I call the same URL in JavaScript the **counter keeps returning 1 every time and at every request** when it should update?