79123122

Date: 2024-10-24 17:48:50
Score: 0.5
Natty:
Report link

Why was this section added?

# Ensure CORS headers are present in all responses
@app.after_request
def after_request(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,PUT,POST,DELETE,OPTIONS')
    return response

I've never personally needed it when using flask-cors.

Their pypi page doesn't seem to suggest you need it either.

from flask import Flask
from flask_cors import CORS

app = Flask(__name__)
CORS(app)

@app.route("/")
def helloWorld():
  return "Hello, cross-origin-world!"

Also, make sure your create_app() is actually called like

app = create_app()

if __name__ == '__main__':
    app.run(debug=True)

and that it is not still

app = Flask(__name__)

Finally, you can try making sure if you remove the CORS or allow all origins with CORS(app, resources={r"/api/*": {"origins": "*"}}) that a curl to your endpoint works as you expect.

curl 127.0.0.1:5000/test
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Starts with a question (0.5): Why was this
  • Low reputation (1):
Posted by: rampollaluis