CORS lets your web app share resources safely with other websites. Flask-CORS helps you set this up easily in Flask apps.
CORS configuration with Flask-CORS
from flask import Flask from flask_cors import CORS app = Flask(__name__) CORS(app, resources={r"/api/*": {"origins": "http://example.com"}})
Use CORS(app) to enable CORS for all routes and origins.
Use resources to limit CORS to certain URL patterns and origins to specify allowed domains.
from flask import Flask from flask_cors import CORS app = Flask(__name__) CORS(app) # Allow all origins for all routes
http://myfrontend.com to URLs starting with /api/ are allowed.CORS(app, resources={r"/api/*": {"origins": "http://myfrontend.com"}})CORS(app, supports_credentials=True)This Flask app allows only requests from http://frontend.example.com to access the /data route. Other origins will be blocked by the browser.
from flask import Flask, jsonify from flask_cors import CORS app = Flask(__name__) # Allow CORS only for frontend.example.com on /data route CORS(app, resources={r"/data/*": {"origins": "http://frontend.example.com"}}) @app.route('/data') def data(): return jsonify({'message': 'Hello from Flask!'}) if __name__ == '__main__': app.run(debug=True)
Browsers enforce CORS, so server must send correct headers for cross-origin requests to work.
Flask-CORS adds these headers automatically based on your config.
Always restrict origins in production to avoid security risks.
CORS controls which websites can access your Flask app resources.
Flask-CORS makes it easy to add CORS headers to your Flask routes.
Configure origins and routes carefully to keep your app safe and accessible.
Practice
Solution
Step 1: Understand what CORS means
CORS stands for Cross-Origin Resource Sharing, which controls which websites can access resources from your Flask app.Step 2: Identify Flask-CORS role
Flask-CORS is a tool to add headers that allow or restrict these cross-origin requests easily.Final Answer:
To allow or restrict cross-origin requests to the Flask app -> Option AQuick Check:
CORS controls access = A [OK]
- Confusing CORS with caching or authentication
- Thinking Flask-CORS speeds up the app
- Assuming Flask-CORS manages databases
Solution
Step 1: Import and create Flask app
Import CORS from flask_cors and create the Flask app instance.Step 2: Enable CORS on the app
Call CORS(app) to enable CORS for all routes. This is the correct syntax.Final Answer:
from flask_cors import CORS\napp = Flask(__name__)\nCORS(app) -> Option CQuick Check:
Enable CORS by passing app to CORS() [OK]
- Calling CORS() without app argument
- Using non-existent app.enable_cors() method
- Setting origins='none' disables all CORS
/data?
from flask import Flask, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app, origins=['https://example.com'])
@app.route('/data')
def data():
return jsonify({'key': 'value'})Solution
Step 1: Check CORS configuration
CORS is enabled with origins=['https://example.com'], so only that origin is allowed cross-origin access.Step 2: Understand route behavior
The /data route returns JSON and inherits the CORS policy set on the app.Final Answer:
Only requests from https://example.com will be allowed cross-origin access -> Option DQuick Check:
Origins list limits access = A [OK]
- Assuming all origins are allowed by default
- Thinking CORS disables all access without explicit allow
- Confusing localhost with example.com
from flask import Flask
from flask_cors import CORS
app = Flask(__name__)
CORS(origins='*')
@app.route('/')
def home():
return 'Hello'Solution
Step 1: Check how CORS is called
CORS() is called without passing the app instance, so it does not enable CORS on the app.Step 2: Understand consequences
Without passing app, no CORS headers are added, so cross-origin requests will fail.Final Answer:
CORS is called without passing the Flask app instance -> Option AQuick Check:
Pass app to CORS() to enable it [OK]
- Forgetting to pass app to CORS()
- Thinking '*' is invalid (it's valid)
- Believing CORS requires JSON response
/api/data for requests from https://trusted.com. Which Flask-CORS setup correctly achieves this?Solution
Step 1: Understand resource-specific CORS
To restrict CORS to a specific route, use the resources parameter with a route pattern and origins.Step 2: Match the route and origin
CORS(app, resources={r'/api/data': {'origins': 'https://trusted.com'}}) correctly sets CORS only on '/api/data' with origins 'https://trusted.com'.Final Answer:
CORS(app, resources={r'/api/data': {'origins': 'https://trusted.com'}}) -> Option BQuick Check:
Use resources to limit routes and origins [OK]
- Setting origins globally instead of per route
- Using wildcard '*' when only one origin is needed
- Using wrong route pattern like '/api/*' for exact match
Ready to prove it?
Pass the test and this pattern moves unknown
