What is CORS and how do you enable it in Flask and FastAPI?
Answer
CORS (Cross-Origin Resource Sharing) allows web browsers to make API requests to a different domain. Flask: use flask-cors: from flask_cors import CORS; CORS(app) allows all origins, or CORS(app, origins=['https://myapp.com']) for specific origins. You can also apply per-route: @cross_origin(). FastAPI: use the built-in CORSMiddleware: from fastapi.middleware.cors import CORSMiddleware; app.add_middleware(CORSMiddleware, allow_origins=['https://myapp.com'], allow_methods=['*'], allow_headers=['*'], allow_credentials=True). In development, allow all origins for convenience. In production, always specify exact allowed origins. CORS does not affect server-to-server calls or tools like Postman — it only affects browser requests to cross-origin APIs.