CORS Explained: Why It Works in Postman but Fails in the Browser

While working with a frontend and backend in a client-server architecture, you may have encountered an error like:
Blocked by CORS policy
You check your API, everything looks correct, and the same endpoint works perfectly in Postman. So why does the browser reject it?
To understand CORS, we first need to understand origins and the Same-Origin Policy.
A Simple Scenario
Suppose we have a frontend application built with React and a backend application built with Spring Boot:
React Frontend
http://localhost:5173
│
│ HTTP Request
▼
Spring Boot Backend
http://localhost:8080
The frontend wants to call:
GET http://localhost:8080/users
The problem is that these two applications have different origins.
But what exactly is an origin?
What Is an Origin?
From the browser's point of view, an origin is made up of three things:
Scheme + Host + Port
For example:
https://example.com:443
has:
Scheme → https
Host → example.com
Port → 443
The path is not part of the origin.
So these URLs have the same origin:
https://example.com/users
https://example.com/products
https://example.com/orders
They all have:
https + example.com + 443
Only the path is different.
But these are different origins:
https://example.com
https://api.example.com
because their hosts are different.
These are also different:
https://example.com
http://example.com
because their schemes are different.
And these are different:
https://example.com:443
https://example.com:8080
because their ports are different.
A simple way to remember it:
Same scheme + same host + same port = same origin.
The Same-Origin Policy
Now imagine you are logged into your banking website.
The browser may have authentication cookies belonging to that bank.
At the same time, you open another website in a different tab.
What if that second website could freely run JavaScript like this?
fetch("https://bank.example.com/accounts")
If the browser allowed the JavaScript to freely read the bank's response, a malicious website could potentially access sensitive information from a website where you are already logged in.
This is why browsers have a security mechanism called the Same-Origin Policy.
The Same-Origin Policy restricts scripts from one origin from freely accessing resources from another origin.
In simple terms:
A website should not automatically be allowed to read data from another origin just because the user's browser can reach that website.
For example:
https://mybank.com
❌
https://malicious-site.com
The JavaScript running on malicious-site.com should not automatically be allowed to read private data from mybank.com.
This isolation is mainly enforced by the browser, because the browser is the environment where many different websites and user sessions coexist.
Then Why Do We Need CORS?
The Same-Origin Policy is useful for security, but legitimate applications also need to communicate across origins.
For example, you might build:
React Frontend
http://localhost:5173
│
│ API Request
▼
Spring Boot Backend
http://localhost:8080
These are different origins because the ports are different.
In production, you might have:
Frontend
https://my-app.vercel.app
│
▼
Backend
https://my-backend.onrender.com
These are also different origins because the hosts are different.
We want these applications to communicate.
This is where CORS (Cross-Origin Resource Sharing) comes in.
CORS is a mechanism that allows a server to tell the browser which other origins are allowed to access its resources.
So we can think of it like this:
Same-Origin Policy
↓
Browser restricts cross-origin access
↓
CORS
↓
Server can explicitly allow trusted origins
How Does CORS Work?
Suppose our React application makes this request:
fetch("http://localhost:8080/users")
The browser knows that the frontend is running at:
http://localhost:5173
and the backend is:
http://localhost:8080
These are different origins.
The browser therefore includes the requesting origin in the HTTP request:
Origin: http://localhost:5173
The backend can respond with:
Access-Control-Allow-Origin: http://localhost:5173
This tells the browser:
"The server allows this origin to access the response."
If the CORS rules are satisfied that is Access-Control-Allow-Origin matches with frontend URL, the browser allows the frontend JavaScript to access the response. If they aren't, the browser reports a CORS error.
The important point is:
The server decides which origins it is willing to allow, and the browser enforces that decision for browser-based JavaScript.
Why Does It Work in Postman but Not in the Browser?
This is one of the most confusing parts of CORS.
Suppose:
Browser
│
│ GET /users
▼
Backend
The browser applies the Same-Origin Policy and CORS rules.
But when you use Postman:
Postman
│
│ GET /users
▼
Backend
Postman is not a browser and does not enforce the browser's Same-Origin Policy, so it does not block your request because of CORS. The same is true for tools such as curl and typical server-to-server requests.
Therefore:
Postman → API
✅ Works
Browser → API
❌ CORS error
This does not necessarily mean that your backend API is broken.
It often means that the browser did not receive the CORS headers required to allow your frontend to access the response.
This distinction is important:
CORS is primarily a browser security mechanism. It is not a restriction imposed on the HTTP server itself.
Same-Origin Request
Let's first see what happens when frontend and backend have the same origin.
Suppose:
Frontend
https://example.com
Backend
https://example.com
The frontend requests:
GET https://example.com/users
Both use:
https + example.com + 443
Therefore, they have the same origin.
The browser doesn't need CORS permission just because the frontend is accessing its own origin.
Browser
│
│ GET /users
▼
Same Origin
https://example.com
│
▼
Response
Cross-Origin Request
Consider:
Frontend
http://localhost:5173
│
│ GET /users
│
▼
Backend
http://localhost:8080
The browser sees that the origins are different.
It sends:
GET /users HTTP/1.1
Origin: http://localhost:5173
If the backend allows the origin, it can respond with:
Access-Control-Allow-Origin: http://localhost:5173
The browser sees that the origin is allowed and makes the response available to the frontend.
The basic flow is:
Browser
│
│ Origin: http://localhost:5173
▼
Backend
│
│ Access-Control-Allow-Origin:
│ http://localhost:5173
▼
Browser
│
▼
JavaScript can access response
What Is a Preflight Request?
Not every cross-origin request requires a preflight.
For certain cross-origin requests, the browser first sends an OPTIONS request to check whether the server allows the intended request. This is called a preflight request.
The browser sends an OPTIONS request first and asks the server:
"Are you allowing this origin and this type of request?"
For example, suppose the frontend wants to send:
DELETE /users/1
The browser may first send:
OPTIONS /users/1
Origin: http://localhost:5173
Access-Control-Request-Method: DELETE
The backend can respond:
Access-Control-Allow-Origin: http://localhost:5173
Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE
The browser checks the response.
If the requested operation is allowed:
OPTIONS
↓
Backend says "Allowed"
↓
DELETE /users/1
If it isn't allowed:
OPTIONS
↓
Backend says "Not allowed"
↓
Browser blocks the actual request
A preflight can also be triggered by certain custom request headers or content types. Therefore, it is better to think of preflight as a browser permission check for requests that aren't considered simple CORS requests, rather than simply "PUT/PATCH/DELETE always require OPTIONS."
You don't normally write this OPTIONS request yourself. The browser handles it automatically.
CORS in Spring Boot
Spring Boot provides built-in support for configuring CORS.
For example, we can configure allowed origins globally:
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**")
.allowedOrigins("http://localhost:5173")
.allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE")
.allowedHeaders("*");
}
}
Now the backend explicitly allows requests from:
http://localhost:5173
We can also configure CORS for a specific controller or endpoint using @CrossOrigin:
@CrossOrigin(origins = "http://localhost:5173")
@GetMapping("/users")
public List<User> getUsers() {
return userService.getUsers();
}
For larger applications, global configuration is often more convenient because you can manage CORS rules in one place.
CORS in Express.js
In Express, the commonly used cors middleware can configure CORS.
Install it:
npm install cors
Then:
import cors from "cors";
app.use(cors({
origin: "http://localhost:5173"
}));
Now the backend allows requests from:
http://localhost:5173
You can also configure specific methods:
app.use(cors({
origin: "http://localhost:5173",
methods: ["GET", "POST", "PUT", "PATCH", "DELETE"]
}));
The middleware adds the appropriate CORS response headers for the browser to evaluate.
A Common Mistake
You may see developers solve CORS problems by simply allowing every origin:
Access-Control-Allow-Origin: *
This can be appropriate for genuinely public, non-credentialed APIs, but it should not be used blindly.
If your application uses credentials such as cookies, the server must use an explicit allowed origin rather than *.
For example:
Access-Control-Allow-Origin: https://my-app.vercel.app
is more appropriate when only your frontend should be allowed to access the API.
Same-Origin vs Cross-Origin
The complete picture looks like this:
Browser
│
┌────────┴────────┐
│ │
Same-Origin Cross-Origin
│ │
▼ ▼
Same-Origin Policy CORS rules
already satisfied checked by browser
│
Backend responds
│
▼
Browser checks headers
│
┌───────┴───────┐
│ │
Allowed Not Allowed
│ │
▼ ▼
JS gets data CORS error
Key Takeaways
- Origin = Scheme + Host + Port
- The path is not part of the origin.
- Browsers use the Same-Origin Policy to isolate websites from each other.
- CORS provides a controlled way for servers to allow cross-origin browser access.
- CORS is primarily enforced by the browser.
- Postman,
curland server-to-server clients don't enforce browser CORS rules. - The server uses headers such as
Access-Control-Allow-Originto tell the browser which origins are allowed. - Some cross-origin requests require an OPTIONS preflight before the actual request.
- CORS is configured on the server/backend.
- Spring Boot and Express both provide ways to configure CORS.
- CORS is not a replacement for authentication or CSRF protection.
Comments
Join the discussionNo comments yet. Be the first to comment.

