Prakhar Goel
Prakhar Goel

Reputation: 1

Ensure CORS response header values are valid - Flask + React + Python

I keep getting the error shown in the image attached.

Inspect element page error

Request headers

I have included the following code in my program but still the error persists:

CORS(app, resources={r"/*": {"origins": "http://localhost:3000"}}, supports_credentials=True)

@app.before_request
def handle_options_request():
    if request.method == 'OPTIONS':
        response = make_response()
        response.headers.add("Access-Control-Allow-Origin", "http://localhost:3000")
        response.headers.add("Access-Control-Allow-Headers", "Content-Type,Authorization")
        response.headers.add("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS")
        response.headers.add("Access-Control-Allow-Credentials", "true")
        return response, 204  # Send 'No Content' for OPTIONS requests

# Apply CORS headers after each request
@app.after_request
def apply_cors(response):
    response.headers.add("Access-Control-Allow-Origin", "http://localhost:3000")
    response.headers.add("Access-Control-Allow-Headers", "Content-Type,Authorization")
    response.headers.add("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS")
    response.headers.add("Access-Control-Allow-Credentials", "true")
    return response

Upvotes: 0

Views: 27

Answers (0)

Related Questions