0

I've written an app that contains an html text input:

<form method="POST" action="" enctype="multipart/form-data">
    <input class="ocsp_override_box_input" type="text" id="ocsp_override" name="ocsp_override" placeholder="Override the AIA field with a URL here..." value="">        
</form>

In the flask app, I access the data with the following:

ocsp_override = request.form.get('ocsp_override')

It works as expected and the rest of my code is able to use this string value and do things with it. So far, so good. The way the code works is that this field should be optional. If it is blank, then it is ignored. If data is filled in it, it runs some checks and then uses this override string later on.

The issue I'm finding is that if I complete the text form using flask dev, it works as expected, but when I reload the page and leave it blank, it appears to be caching the previous entry.

For instance, if I enter in http://example.com in the text field, the code will use that string. If I reload the page (not using F5, by reloading the get page so I can post again), and I leave the field blank, the code appears to be trying to use http://example.com again. What's weird is that if I try to print(request.form.get('ocsp_override')) then it shows nothing.

If I wait a length of time (I want to say 10 mins), and then try again, everything works as expected with the field blank.

Is there some kind of caching happening here and how can I force it to reload and be fresh every time?

Eddi
  • 167
  • 1
  • 9
  • Provide the code – darth baba Feb 18 '23 at 19:52
  • I can provide some code, but there's a lot of it surrounding this issue that is not pertinent to the issue. Give me a day or two and I'll see if I can provide a code chunk that should simplify it. Might also use this opportunity to get some other tests in to ensure it's not me being stupid. – Eddi Feb 19 '23 at 20:14

1 Answers1

0

You might try disabling autocomplete:

e.g.

<input class="ocsp_override_box_input" ... autocomplete="off">

And if that does not work, you can try adding a cache control header to the response to prevent the browser from caching the page:

response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
response.headers['Pragma'] = 'no-cache'
response.headers['Expires'] = '0'
return response

See this answer describing Cache-Control vs Pragma headers.

Increasingly Idiotic
  • 5,700
  • 5
  • 35
  • 73
  • I read about this and tried to implement it, but I couldn't quite figure out how to connect it to my request.form code. I'll do some digging on it and will give this a try and let you know. – Eddi Feb 19 '23 at 20:18
  • I tried to add this with the following: ``` @app.after_request def add_no_cache_headers(response): response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate' response.headers['Pragma'] = 'no-cache' response.headers['Expires'] = '0' return response ``` It still did not work. Is this correct? add_no_cache_headers(response) is a function so I need to call it? – Eddi Feb 20 '23 at 01:28
  • I also tried to add the `response = Flask.make_response(return_render(site_vars = site_vars))` followed by your `response.headers` as suggested above, and then just `return response` when needed. The issue is that my `site_vars = site_vars` is a dictionary with all my variables and Flask doesn't seem to want to pass that info onwards. If I use `return_render` without `make_response` then it works fine. – Eddi Feb 20 '23 at 01:53