<?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: Nilotpal Baishya</title>
    <description>The latest articles on DEV Community by Nilotpal Baishya (@bynilotpal).</description>
    <link>https://dev.to/bynilotpal</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%2F3965951%2Faaea1422-1fc3-4134-bde7-a7b0d5c9bb33.jpg</url>
      <title>DEV Community: Nilotpal Baishya</title>
      <link>https://dev.to/bynilotpal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bynilotpal"/>
    <language>en</language>
    <item>
      <title>My First CORS Error in FastAPI</title>
      <dc:creator>Nilotpal Baishya</dc:creator>
      <pubDate>Wed, 03 Jun 2026 10:57:54 +0000</pubDate>
      <link>https://dev.to/bynilotpal/my-first-cors-error-in-fastapi-45jo</link>
      <guid>https://dev.to/bynilotpal/my-first-cors-error-in-fastapi-45jo</guid>
      <description>&lt;p&gt;I'm learning FastAPI. And I am not a person who can learn by watching tutorials.&lt;/p&gt;

&lt;p&gt;So I decided to learn while building. So I built something small just to start with FastAPI. A basic expense tracker.&lt;/p&gt;

&lt;p&gt;Nothing fancy. Just a FastAPI backend with a few endpoints and a simple HTML page with JavaScript. There are three buttons in it: Load Expenses, Add Expense, Calculate Total. I just wanted to see how frontend talks to backend.&lt;/p&gt;

&lt;h3&gt;
  
  
  Everything seemed fine
&lt;/h3&gt;

&lt;p&gt;I started the server. No errors. Tested my API in Swagger UI. Worked perfectly. Then I opened my HTML file. Clicked "Load Expenses."&lt;/p&gt;

&lt;p&gt;But there was nothing. like no expenses, no error, just nothing.&lt;/p&gt;

&lt;p&gt;I stared at the screen thinking &lt;em&gt;What did I do wrong?&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  I blamed my backend first
&lt;/h3&gt;

&lt;p&gt;I thought I messed up my Python code. I checked everything but there was nothing wrong. Then tested Swagger UI again. The endpoint was fine.&lt;/p&gt;

&lt;h3&gt;
  
  
  Then I blamed my frontend
&lt;/h3&gt;

&lt;p&gt;So I went to check my fetch request. It looked fine to me. I checked the button. Worked fine. But after all this there was still nothing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Then I opened the browser console
&lt;/h3&gt;

&lt;p&gt;This was the moment. I clicked F12 and saw this red error:&lt;/p&gt;

&lt;p&gt;Access to fetch at '&lt;a href="http://127.0.0.1:8000/expenses" rel="noopener noreferrer"&gt;http://127.0.0.1:8000/expenses&lt;/a&gt;' from origin '&lt;a href="http://127.0.0.1:3000" rel="noopener noreferrer"&gt;http://127.0.0.1:3000&lt;/a&gt;' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.&lt;/p&gt;

&lt;p&gt;I had no idea what CORS meant.&lt;/p&gt;

&lt;h3&gt;
  
  
  What I learned
&lt;/h3&gt;

&lt;p&gt;I learnt that my backend was working the whole time. I searched about CORS error and came to know about what is CORS and all. The browser was blocking it. SO my frontend was on port 3000 and my backend was on port 8000.They are different ports. The browser saw them as different "origins" and said "nope, not allowed unless the backend gives permission." &lt;br&gt;
My backend wasn't giving permission. So I added the CORS middleware, it adds special HTTP headers to my backend responses.&lt;/p&gt;
&lt;h3&gt;
  
  
  The fix
&lt;/h3&gt;

&lt;p&gt;I added this to my FastAPI code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi.middleware.cors&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;CORSMiddleware&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_middleware&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;CORSMiddleware&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;allow_origins&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;http://127.0.0.1:3000&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;allow_methods&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;*&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;allow_headers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;*&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What stuck with me
&lt;/h3&gt;

&lt;p&gt;The fix was four lines. But what I actually learned was:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;I should always open the browser console FIRST. At least not after checking everything else.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Test my API separately before blaming it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CORS errors are browser things, not code bugs.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>api</category>
      <category>fastapi</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
