Title: Flask - problem with sending POST query due to browser session
Tags: javascript;python;flask
Question: I have an app which uses Flask as a server, I use JavaScript in front-end and Python in back-end (flask). I have a problem with sending data using REST API from flask into JS.
The issue is that the users are receiving another users data. Here's hypothetical situation:
user A starts a new browser session and when he clicks he receives users A data (everything is OK at this point),
user B starts a new browser session and when he clicks he receives users B data (everything is OK at this point as well),
user A refreshes a browser and when he clicks he receives users B data and that is the problem
As you can see everything is working correctly but only if the user starts a new browser session. When the user refreshes the browser it all goes wrong.
The app has a few hundred lines of code so let me show you the most important part of the code.
Here is my simplified JS code:
document.getElementById('geolocate').addEventListener('click', event =&gt; {
const option ={
method: 'GET',
headers: {
'content-type': 'text/plain',
'cache-control': 'no-cache',
'cache': &quot;no-store&quot;
}
fetch('myUrl/api/v1/myEndpoint', option)
.then((response) =&gt; {
return response.text();
});
As you can see, it is just a simple event with fetch which is a receiver of POSTed data from backend.
Here you can see Flask code:
@app.route('myUrl/api/v1/myEndpoint', methods=['GET', 'POST', 'OPTIONS'])
def db_cords():
with open('/var/www/html/test.txt', 'r') as our_file:
for line in our_file:
return line
It reads the first line of our /var/www/html/test.txt file and sends it to the endpoint.
Here you can see the configuration of Flask:
app = Flask(__name__)
app.debug = False
app._static_folder = os.path.abspath(&quot;templates/static/&quot;)
config = {
&quot;DEBUG&quot;: True,  # some Flask specific configs
&quot;CACHE_TYPE&quot;: &quot;SimpleCache&quot;,  # Flask-Caching related configs
&quot;CACHE_DEFAULT_TIMEOUT&quot;: 10
# tell Flask to use the above defined config
app.config.from_mapping(config)
if __name__ == '__main__':
log = logging.getLogger('werkzeug')
log.disabled = True
app.run(host, port, debug, ssl_context=context)
I tried to add no-cache and simple-cache flags in headers but it did not help. Also I tried to use a decorator @after_request.
Everything in the app is working just fine. The only problem I have is the browsing session. Do you guys have any idea how to solve my issue?
Comment: @Anonymous, I think the good way would be like this, what do you think?
`app = Flask(__name__)
app.debug = False
app._static_folder = os.path.abspath("templates/static/")
config = {
"DEBUG": True,  # some Flask specific configs
"SESSION_REFRESH_EACH_REQUEST": True # Flask-Caching related configs
# tell Flask to use the above defined config
app.config.from_mapping(config)`

Comment: @Anonymous The data is latitude and longitude of a specific point (basically a nearest location from the user). What I think may cause the problem is the fact that the POST query is executed only once in Flask code and because of that it doesn't show proper data when the user clicks 'geolocate' button more than once. Perhaps it should be executed every time user clicks the button but I don't know how to execute backed methods simultaneously with JS event. Also, user doesn't need to log first.

Comment: I don't see any session mechanism here, are users supposed to log in first ? What exactly is the data that is sent to users ?



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:04/04/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><python><flask>', 'Title': 'Flask - problem with sending POST query due to browser session', 'ViewCount': 79, 'id': 66941217, 'user_AboutMe': None, 'user_AccountId': 19491744.0, 'user_DisplayName': 'macpan', 'user_Reputation': 93.0, 'user_Views': 26.0, 'user_id': 14259825.0}}