From 927fb91bb3681d09f7d504340787c48447862580 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=7C=C2=B0=5F=C2=B0=7C?= Date: Sat, 7 Dec 2024 14:34:33 +0100 Subject: [PATCH] fix: check before request CORS preflight channel did not succeed https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSPreflightDidNotSucceed Answer used https://github.com/corydolphin/flask-cors/issues/292#issuecomment-2027157404 --- backend/matcha/__init__.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/backend/matcha/__init__.py b/backend/matcha/__init__.py index 4fde57c3..7e87ba1f 100644 --- a/backend/matcha/__init__.py +++ b/backend/matcha/__init__.py @@ -1,4 +1,4 @@ -from flask import Flask +from flask import Flask, request, jsonify from flask_socketio import SocketIO from flask_cors import CORS from os import environ @@ -42,4 +42,14 @@ def create_app(): socketio.on_namespace(MainNamespace("/")) + @app.before_request + def before_request(): + headers = { + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS", + "Access-Control-Allow-Headers": "Content-Type", + } + if request.method.lower() == "options": + return jsonify(headers), 200 + return app