<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alvooh</title>
    <description>The latest articles on DEV Community by Alvooh (@westsidedev).</description>
    <link>https://dev.to/westsidedev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1361427%2F01e54a23-ccf6-4280-9fca-6c0efae6b888.jpeg</url>
      <title>DEV Community: Alvooh</title>
      <link>https://dev.to/westsidedev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/westsidedev"/>
    <language>en</language>
    <item>
      <title>Fixing CORS Issues in a Flask-React App</title>
      <dc:creator>Alvooh</dc:creator>
      <pubDate>Mon, 10 Mar 2025 02:16:52 +0000</pubDate>
      <link>https://dev.to/westsidedev/fixing-cors-issues-in-a-flask-react-app-2ndj</link>
      <guid>https://dev.to/westsidedev/fixing-cors-issues-in-a-flask-react-app-2ndj</guid>
      <description>&lt;p&gt;Cross-Origin Resource Sharing (CORS) issues are a common headache when integrating a Flask backend with a React frontend. If you've encountered errors like:&lt;/p&gt;

&lt;p&gt;Access to fetch at '&lt;a href="http://localhost:5000/api/data" rel="noopener noreferrer"&gt;http://localhost:5000/api/data&lt;/a&gt;' from origin '&lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;' has been blocked by CORS policy.&lt;/p&gt;

&lt;p&gt;Then you’re not alone! In this post, I'll walk you through what CORS is, why this happens, and how to fix it.&lt;/p&gt;

&lt;p&gt;What is CORS?&lt;/p&gt;

&lt;p&gt;CORS is a security feature implemented by web browsers to prevent unauthorized access to resources from different origins. An "origin" consists of the protocol (http/https), domain, and port. If your React frontend (&lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;) tries to fetch data from a Flask backend (&lt;a href="http://localhost:5000" rel="noopener noreferrer"&gt;http://localhost:5000&lt;/a&gt;), the browser blocks the request unless the server explicitly allows it.&lt;/p&gt;

&lt;p&gt;How to Fix CORS Issues in Flask&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install Flask-CORS&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Flask does not handle CORS by default. To enable it, install the flask-cors package:&lt;/p&gt;

&lt;p&gt;pip install flask-cors&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Enable CORS in Your Flask App&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Modify your Flask app to include CORS support:&lt;/p&gt;

&lt;p&gt;from flask import Flask, jsonify&lt;br&gt;
from flask_cors import CORS&lt;/p&gt;

&lt;p&gt;app = Flask(&lt;strong&gt;name&lt;/strong&gt;)&lt;br&gt;
CORS(app)  # Enables CORS for all routes&lt;/p&gt;

&lt;p&gt;@app.route("/api/data")&lt;br&gt;
def get_data():&lt;br&gt;
    return jsonify({"message": "CORS is now enabled!"})&lt;/p&gt;

&lt;p&gt;if &lt;strong&gt;name&lt;/strong&gt; == "&lt;strong&gt;main&lt;/strong&gt;":&lt;br&gt;
    app.run(debug=True)&lt;/p&gt;

&lt;p&gt;This allows requests from any origin. If you want to restrict access, specify allowed origins like this:&lt;/p&gt;

&lt;p&gt;CORS(app, origins=["&lt;a href="http://localhost:3000%22%5D" rel="noopener noreferrer"&gt;http://localhost:3000"]&lt;/a&gt;)  # Allow only requests from React frontend&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Enabling CORS for Specific Routes&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you prefer more control, apply CORS to specific routes:&lt;/p&gt;

&lt;p&gt;from flask_cors import cross_origin&lt;/p&gt;

&lt;p&gt;@app.route("/api/data")&lt;br&gt;
@cross_origin(origin="&lt;a href="http://localhost:3000%22" rel="noopener noreferrer"&gt;http://localhost:3000"&lt;/a&gt;)&lt;br&gt;
def get_data():&lt;br&gt;
    return jsonify({"message": "CORS applied to this route only"})&lt;/p&gt;

&lt;p&gt;Handling CORS on the React Side&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using Proxy in Development&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In your React project’s package.json, add:&lt;/p&gt;

&lt;p&gt;"proxy": "&lt;a href="http://localhost:5000" rel="noopener noreferrer"&gt;http://localhost:5000&lt;/a&gt;"&lt;/p&gt;

&lt;p&gt;This tells React to forward API requests to Flask without triggering CORS issues.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using Fetch with CORS Options&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you’re making API calls manually, include CORS options:&lt;/p&gt;

&lt;p&gt;fetch("&lt;a href="http://localhost:5000/api/data" rel="noopener noreferrer"&gt;http://localhost:5000/api/data&lt;/a&gt;", {&lt;br&gt;
  method: "GET",&lt;br&gt;
  headers: {&lt;br&gt;
    "Content-Type": "application/json",&lt;br&gt;
  },&lt;br&gt;
  mode: "cors"&lt;br&gt;
})&lt;br&gt;
  .then(response =&amp;gt; response.json())&lt;br&gt;
  .then(data =&amp;gt; console.log(data))&lt;br&gt;
  .catch(error =&amp;gt; console.error("Error:", error));&lt;/p&gt;

&lt;p&gt;Conclusion&lt;/p&gt;

&lt;p&gt;CORS issues can be frustrating, but they are easy to fix with the right approach. By using Flask-CORS and configuring it correctly, you can ensure smooth communication between your Flask backend and React frontend. If you found this helpful, let me know in the comments or share your own experience with CORS.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
