/* Route-Critical (index, V6 §3.6.3 — erzeugt von tools/generate_critical_css.mjs) */ .collapse:not(.show){display:none}.navbar{display:flex;flex-wrap:wrap;align-items:center;justify-content:space-between}.navbar>.container-fluid{display:flex;flex-wrap:inherit;align-items:center;justify-content:space-between}.mydraft-svg-icon{display:inline-block;vertical-align:middle;color:currentcolor;flex-shrink:0;transition:color var(--duration-fast, 150ms) ease,stroke var(--duration-fast, 150ms) ease,fill var(--duration-fast, 150ms) ease}.mydraft-svg-icon circle,.mydraft-svg-icon line,.mydraft-svg-icon path,.mydraft-svg-icon polygon,.mydraft-svg-icon polyline,.mydraft-svg-icon rect{transition:stroke var(--duration-fast, 150ms) ease,fill var(--duration-fast, 150ms) ease}@layer components{.live-pulse-dot{width:8px;height:8px}}.min-w-0{min-width:0}@layer overrides{.live-radar-header-badge{background:rgba(239,68,68,.12);border:1px solid rgba(239,68,68,.35);color:#fca5a5;font-size:.75rem;font-weight:700;transition:.25s;cursor:pointer}}@layer overrides{.live-pulse-dot{width:8px;height:8px;background-color:var(--threat-critical,#ef4444);border-radius:50%;display:inline-block;box-shadow:rgba(239,68,68,.7) 0 0 0 0}}@layer overrides{body.light .live-radar-header-badge,html[data-bs-theme=light] .live-radar-header-badge{background:#fbd9dd;border-color:#eeb2b8;color:#8a1723!important}}.isharestuff-live-ticker{color:var(--color-text-body,#f8fafc);backdrop-filter:none;flex-wrap:wrap;row-gap:.3rem;background:var(--color-surface,rgba(15,23,42,.85))!important;border:1px solid var(--color-border,rgba(255,255,255,.12))!important;padding:.45rem .5rem!important;margin-bottom:.75rem!important}.ticker-content{mask-image:linear-gradient(to right,transparent 0px,rgb(0,0,0) 14px,rgb(0,0,0) calc(100% - 14px),transparent 100%);touch-action:pan-y;cursor:grab;user-select:none;-webkit-tap-highlight-color:transparent;order:1;width:100%;max-width:100%;flex:0 0 100%!important}.isharestuff-live-ticker .ticker-lead-group{order:2;width:100%;justify-content:space-between;margin-right:0!important}.ticker-track{display:flex;align-items:center;white-space:nowrap;will-change:transform;transform:translate3d(0,0,0);--ticker-speed:1;animation-name:isharestuffTicker;animation-timing-function:linear;animation-iteration-count:infinite;animation-duration:calc(26s / var(--ticker-speed, 1))}.ticker-link{max-width:min(78vw,340px);font-size:.82rem;display:inline-block;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;vertical-align:baseline;transition:color .2s;color:var(--color-text-heading,#f8fafc)!important}.isharestuff-live-ticker .ticker-ctrl-btn{min-width:44px;height:44px;font-size:.78rem;-webkit-tap-highlight-color:transparent}.ticker-controls-bar{gap:.4rem}.ticker-counter{font-family:var(--bs-font-monospace, ui-monospace, monospace);font-size:.72rem;line-height:1;color:var(--color-text-secondary,#94a3b8);min-width:3.4em;text-align:center;user-select:none;white-space:nowrap;margin-left:auto}.ticker-item{font-size:.82rem}.ticker-item .ticker-date{font-size:.72rem;vertical-align:baseline}.ticker-text-group{display:inline-flex;align-items:baseline}.ticker-badge-live-btn{border:0;cursor:pointer;text-decoration:none!important}.ticker-badge{font-size:.78rem!important;min-height:44px!important;min-width:44px!important;padding:6px 12px!important;display:inline-flex!important;align-items:center!important;justify-content:center!important}.signal-live-strip{display:flex;align-items:center;gap:.625rem;min-height:44px;margin-bottom:1rem;padding:.4rem .875rem;border-radius:9999px;border:1px solid rgba(var(--color-border-rgb,148,163,184),.35);background:rgba(var(--color-surface-rgb,15,23,42),.6);backdrop-filter:blur(12px) saturate(140%);overflow:hidden}.signal-live-badge{display:inline-flex;align-items:center;gap:.35rem;flex:0 0 auto;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:.68rem;font-weight:700;letter-spacing:.08em;text-transform:uppercase;color:#fda4af}[data-bs-theme=light] .signal-live-badge,body.light .signal-live-badge{color:#be123c}.signal-live-dot{width:8px;height:8px;border-radius:50%;background:#f43f5e;animation:1.6s ease-in-out 0s infinite normal none running signalLivePulse}.signal-live-strip.is-online .signal-live-dot{background:#22c55e;animation:auto ease 0s 1 normal none running none}.signal-live-item{font-size:.82rem;line-height:1.35;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;min-width:0}.menue-widget .nav-tree-link{min-width:44px;display:flex;align-items:center;word-break:break-word;overflow-wrap:anywhere;max-width:100%;min-height:44px!important}.menue-widget .nav-title-text{overflow-wrap:anywhere;word-break:break-word}.menue-widget .menue-widget-btn{min-height:44px;min-width:44px;display:inline-flex;align-items:center;justify-content:center}@media (min-width:992px){.navbar-expand-lg{flex-wrap:nowrap;justify-content:flex-start}.navbar-expand-lg .navbar-toggler{display:none}}@media (prefers-reduced-motion:reduce){@layer overrides{#news-popup,#news-toast-banner,.animate-pulse,.live-pulse-dot,.live-pulsing-dot,.news-item-card{animation:auto ease 0s 1 normal none running none;transition:none}}.ticker-track{animation:auto ease 0s 1 normal none running none!important}.ticker-content{cursor:default}.signal-live-dot{animation:auto ease 0s 1 normal none running none}}@media (min-width:768px){.isharestuff-live-ticker{flex-wrap:nowrap;row-gap:0;backdrop-filter:blur(12px);padding:.5rem!important;margin-bottom:1rem!important}.isharestuff-live-ticker .ticker-content{order:0;width:auto;max-width:none;flex:1 1 auto!important}.isharestuff-live-ticker .ticker-lead-group{order:0;width:auto;justify-content:flex-start;margin-right:1rem!important}.ticker-controls-bar{gap:.3rem}.isharestuff-live-ticker .ticker-ctrl-btn{min-width:30px;height:auto;font-size:inherit}.ticker-counter{font-size:.66rem;margin-left:0}.ticker-track{animation-duration:calc(40s / var(--ticker-speed, 1))}.ticker-link{max-width:340px;font-size:.85rem}.ticker-item{font-size:.85rem}.ticker-badge{font-size:inherit!important;min-height:0!important;min-width:0!important;padding:.5rem 1rem!important}}@media (max-width:359.98px){.ticker-counter{display:none}.ticker-controls-bar{gap:.3rem}.ticker-badge{padding:6px 9px!important}}@media (max-width:767.98px){.signal-live-strip{flex-wrap:wrap;align-items:flex-start;border-radius:1rem;padding:.5rem .75rem;row-gap:.25rem}.signal-live-badge{margin-top:.1rem}.signal-live-item{white-space:normal;display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;text-overflow:clip;overflow-wrap:anywhere;line-height:1.3}} 🔒 My First CORS Error in FastAPI | Sichere Programmierung – TSECURITY.DE Zum Hauptinhalt springen
•
IT NachrichtenApple überholt Samsung bei Netzbetreiber-Verkäufen(17.10.2026 um 20:00 Uhr)
••••••••
Sichere ProgrammierungPrice Pulse AI - Competitive Radar(17.10.2026 um 21:11 Uhr)
••
IT NachrichtenApple überholt Samsung bei Netzbetreiber-Verkäufen(17.10.2026 um 20:00 Uhr)
••••••••
Sichere ProgrammierungPrice Pulse AI - Competitive Radar(17.10.2026 um 21:11 Uhr)
•
Intelligence View
⚡ tsecurity.de Intelligence

My First CORS Error in FastAPI

I'm learning FastAPI. And I am not a person who can learn by watching tutorials. So I decided to learn while building. So I built something small just to start…

Beitrag
0
Seite
0
↗ Quelle (dev.to)
Social ReaktionenReagiere als Erste:r — dein Feedback zählt!

I'm learning FastAPI. And I am not a person who can learn by watching tutorials.



So I decided to learn while building. So I built something small just to start with FastAPI. A basic expense tracker.



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.






Everything seemed fine



I started the server. No errors. Tested my API in Swagger UI. Worked perfectly. Then I opened my HTML file. Clicked "Load Expenses."



But there was nothing. like no expenses, no error, just nothing.



I stared at the screen thinking What did I do wrong?






I blamed my backend first



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.






Then I blamed my frontend



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.






Then I opened the browser console



This was the moment. I clicked F12 and saw this red error:



Access to fetch at 'http://127.0.0.1:8000/expenses' from origin 'http://127.0.0.1:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.



I had no idea what CORS meant.






What I learned



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."

My backend wasn't giving permission. So I added the CORS middleware, it adds special HTTP headers to my backend responses.





The fix



I added this to my FastAPI code:




from fastapi.middleware.cors import CORSMiddleware

app.add_middleware(
CORSMiddleware,
allow_origins=["http://127.0.0.1:3000"],
allow_methods=["*"],
allow_headers=["*"],
)









What stuck with me



The fix was four lines. But what I actually learned was:




  1. I should always open the browser console FIRST. At least not after checking everything else.


  2. Test my API separately before blaming it.


  3. CORS errors are browser things, not code bugs.


Intelligence Digest — kostenlos Täglich die wichtigsten Security-News · jederzeit abbestellbar
Community Rating & Social Proof
⭐ Leser-Wertung
–
Ø / 5.0
Noch keine Leser-Bewertungen
War diese Seite hilfreich?
0
Jetzt Bewertung abgeben
🟢 Niedrig (1-3.9) 🟡 Mittel (4.0-6.9) 🔴 Hoch (7.0-8.9) Kritisch (9.0-10.0)

Verwandte Story-Cluster & Quellen (Vektor-KI)

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten My First CORS Error in FastAPI

Thematisch verwandte Begriffe: First, CORS, Error, FastAPI · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

💬 Kommentare werden geladen…
Zum Aktualisieren ziehen
Nächster Beitrag