lochana.Back to portfolio
API client · DevTools

Fix "Authorization is not allowed by Access-Control-Allow-Headers"

The browser’s preflight asks if it may send Authorization. Add authorization to the server’s Access-Control-Allow-Headers, not to the request.

  • Beginner
  • ~6 min
  • 4 steps
  • Runs in your browser

What you’ll practise

0/3
  • Find which header the browser asked permission for.
  • Fix it where permission is granted: the server.
  • Apply the rule to a different header.

Before you start

  • A browser page can call an HTTP API
  • Basic request and response headers
HANDS-ON WORKSPACE

Simulated API client · DevTools

Guided mode
STEP 01 / 04

See what the browser blocks.

Your goal: make the profile page load now that it sends a sign-in token. In Browser, click Reload page, then read the error in Console.

Step 1 of 4 · Reproduce the error SIMULATION
Browser · profile page API on localhost:4000

Reload the page to load your profile.

Nothing yet. Reload the page.

Learn at your own pace.

TAKE IT INTO THE REAL WORLD

How this works in real projects

The ideas behind this lesson, with the official documentation to read next.

01

The preflight asks about each header

Before a request with headers such as Authorization, the browser sends an OPTIONS preflight that lists them in Access-Control-Request-Headers. Postman and curl skip this step, which is why the same request works there.

MDN: Preflight requestdeveloper.mozilla.org
02

The server must allow each one

Every header the preflight lists must appear in the response’s Access-Control-Allow-Headers. Header names are case-insensitive. A * wildcard never covers Authorization: list it by name.

MDN: Access-Control-Allow-Headersdeveloper.mozilla.org
03

Fix the server, not the request

Access-Control-* headers are response headers. Adding them to your fetch call only gives the preflight another header to ask about. Change the API’s CORS configuration instead.

KEEP PRACTISING

Try another lesson

All lessons
API client · DevToolsBeginner

APIs & HTTP

Fix a CORS error when the API works in Postman

Postman does not enforce browser CORS. Read the browser’s Origin header, then configure the API to allow that exact origin. With credentials, do not use `*`.

~7 minStart lesson
Docker DesktopBeginner

Containers & Docker

Fix an app container that can't reach its database

Your app container restarts with ECONNREFUSED 127.0.0.1:5432 while Postgres runs. Inside a container, localhost is that container: connect to the db service on 5432.

~7 minStart lesson