Reputation: 1060
First, I'm a sysadmin, not a developer, but I am assisting a developer with setting up a Flask app. The app works in that it serves content and looks great. However, any time we need to take an action that involves an update (POST), we get 404s to the POST url. We experience this issue on Apache, but NOT on the barebones Flask dev server.
Apache is serving the content over TCP/443; we can access the app, but our checkbox/text field changes do not persist, presumably because of the 404s.
For example:
xx.xx.xx.xx - - [22/Oct/2024:15:24:14 -0400] "POST /update-status HTTP/1.1" 404 196 "https://<some domain>/image_viewer/3204405376527941" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:131.0) Gecko/20100101 Firefox/131.0"
The app has the route defined:
# Route to handle the "Image" checkbox update
@app.route('/update-status', methods=['POST'])
def update_image():
if 'user_id' not in session:
return jsonify({"error": "Unauthorized"}), 401
data = request.get_json()
image_id = data.get('image_id')
image = data.get('image', 0)
try:
update_image_status(image_id, "image", image)
return jsonify({"success": True}), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
Here is the piece of the template where the user clicks the checkbox that isn't persisting:
<!-- "Image" Checkbox -->
<div>
<input type="checkbox" id="image-{{ image.image_id }}" name="image-{{ image.image_id }}"
{% if image.image %}checked{% endif %} onchange="updateImageStatus('{{ image.image_id }}', this.checked)">
<label class="checkbox-label" for="image-{{ image.image_id }}">Image: maps, photos, etc.</label>
</div>
Here is my Apache ssl.conf config:
<VirtualHost *:443>
# General setup for the virtual host, inherited from global configuration
ServerName hostname.domain.com
DocumentRoot /srv/flask_apps/flask_document_image_viewer
# app_doc_image_viewer
WSGIDaemonProcess app_doc_image_viewer processes=2 threads=15
WSGIProcessGroup app_doc_image_viewer
WSGIScriptAlias /app_doc_image_viewer /srv/flask_apps/flask_document_image_viewer/flask_app.wsgi
<Directory /srv/flask_apps/flask_document_image_viewer>
AllowOverride all
Require all granted
</Directory>
ErrorLog /var/log/httpd/ssl_error.log
CustomLog /var/log/httpd/ssl_access.log combined
</VirtualHost>
Here is my flask_app.wsgi:
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/srv/flask_apps/flask_document_image_viewer")
from app_doc_image_viewer import app as application
Running Python 3.9.18, Apache 2.4
We are pulling our hair out over this. I've seen similar posts with this 404 question, but I've investigated those solutions with no success.
What do we need to do to get these POST requests to work???
Any help is appreciated.
Update: 10/28/2024
The issue is with the routing and Apache config.
It works when I move the Flask app to the web server DocRoot (/). However, if it is in a subdirectory (/app_doc_image_viewer), I get the 404s.
This brings me to my Apache VirtualHost configuration, which seems correct (see above), but I may be overlooking something.
To support multiple Flask apps down the road, the app needs to work from a subdirectory in its own Virtual Host, without asking the developer to prepend a path to the routes in their code.
I am following this example but without success.
Any help is appreciated.
Thanks!
Upvotes: 0
Views: 37