Here's my front-end that's sending the data:
async function notifyBackendAboutUpload(fileName) { try { const response = await fetch('http://127.0.0.1:5000/notify-upload', { method: 'POST', headers: {'Content-Type': 'application/json', }, body: JSON.stringify({ fileName }), credentials: 'include', }); const responseData = await response.json(); console.log(responseData); } catch (error) { console.error('Error notifying the backend:', error); }
Here's my __init__.py
:
from flask import Flask, sessionfrom flask_cors import CORSfrom dotenv import load_dotenvimport osdef create_app(): load_dotenv() # Load environment variables app = Flask(__name__) # Configuration from environment variables app.config["AWS_ACCESS_KEY_ID"] = os.getenv("AWS_ACCESS_KEY_ID") app.config["AWS_SECRET_ACCESS_KEY"] = os.getenv("AWS_SECRET_ACCESS_KEY") app.config["AWS_REGION"] = os.getenv("AWS_REGION") app.config["SECRET_KEY"] = os.getenv("SECRET_KEY") app.config["SESSION_COOKIE_SAMESITE"] = "None" CORS(app, supports_credentials=True) from app.routes.routes import routes as routes_blueprint app.register_blueprint(routes_blueprint) # Potentially add more blueprint registrations here return app
Here's my routes.py
:
from flask import Blueprint, jsonify, request, session, redirect, url_forfrom flask_cors import CORSfrom app.services.aws_service import fetch_csv_from_s3routes = Blueprint("routes", __name__)CORS(routes, supports_credentials=True)@routes.route("/test-s3-fetch")def test_s3_fetch(): bucket_name = "instanalytics" object_key = session.get("fileName") print('Retrieved from session:', object_key) if not object_key: return jsonify({'error': 'No file name provided'}), 400 # df = fetch_csv_from_s3(bucket_name, object_key) return jsonify({"message": f"FileName from session: {object_key}"}) # return jsonify(df.head().to_dict())@routes.route('/notify-upload', methods=['POST'])def notify_upload(): data=request.get_json() fileName=data['fileName'] if not fileName: return jsonify({'error': 'Filename is required'}), 400 session['fileName'] = fileName print('Stored in session:', session.get('fileName')) return redirect(url_for("routes.test_s3_fetch"))@routes.route("/test-session")def test_session(): # Directly manipulate and check session data session["test"] = "Session is working!" print("Session data:", session) return jsonify({"testSessionValue": session.get("test")})
Now, I'm trying to use the sent file name from the front-end to use here and it's storing the fileName
fine but it's not retrieving it correctly for some reason. Here's the logs:
Stored in session: aggregate.csv127.0.0.1 - - [04/Apr/2024 05:47:18] "POST /notify-upload HTTP/1.1" 302 -Retrieved from session: None127.0.0.1 - - [04/Apr/2024 05:47:18] "GET /test-s3-fetch HTTP/1.1" 400 -
I'm new to flask so please let me know what I'm doing wrong. Thanks.