Why an API Call Works in cURL but Fails in the Browser (CORS)
Understand CORS errors and preflight requests, tell them apart from network failures, and fix them on your API, with Spring Boot and Express examples.
The same request, two different rules
cURL is a program on your computer, so it can call any server and read any response. A web page is different: it runs code from one origin (scheme, host and port) and the browser only lets it read a response from another origin if that server agrees. This is the same-origin policy, and Cross-Origin Resource Sharing (CORS) is how a server agrees.
That is why a request copied from documentation works in a terminal and fails in a browser-based tool or in your front end. The request itself is fine; the response simply lacks the headers that allow a page on another origin to read it. Browsers deliberately report only a generic error such as "Failed to fetch" or "NetworkError", so pages cannot probe other servers.
Simple requests and preflight requests
A GET, HEAD or POST with only safelisted headers (Accept, Accept-Language, Content-Language, and a Content-Type of text/plain, multipart/form-data or application/x-www-form-urlencoded) is a simple request. The browser sends it and then checks the response for Access-Control-Allow-Origin.
Anything else, such as PUT, PATCH or DELETE, an Authorization or X-API-Key header, or Content-Type: application/json, first triggers a preflight: an OPTIONS request asking whether the real request is allowed. The server must answer the preflight with Access-Control-Allow-Methods and Access-Control-Allow-Headers that cover the real request, or the browser never sends it.
OPTIONS /api/orders HTTP/1.1
Origin: https://app.example.com
Access-Control-Request-Method: POST
Access-Control-Request-Headers: authorization, content-type
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Headers: authorization, content-type
Access-Control-Max-Age: 600Is it CORS or is the server down?
Both cases look identical to a web page. To tell them apart, check whether the server answers at all. In the cURL executor, select Check if the server is reachable after a failed request: it sends an opaque no-cors request whose response cannot be read but whose arrival can be detected. If the server answered, CORS is the blocker. If not, look at the host name, whether the server is running, its TLS certificate, mixed content (an http:// URL from an https:// page) and browser extensions.
The browser's developer tools show the real reason in the Console, for example "No 'Access-Control-Allow-Origin' header is present on the requested resource" or "Response to preflight request doesn't pass access control check".
Fix it on a Spring Boot API
Register allowed origins once with a WebMvcConfigurer bean (or @CrossOrigin on a controller). If you use Spring Security, also call http.cors(Customizer.withDefaults()) so preflight requests are not rejected before they reach your controllers. List explicit origins; a wildcard cannot be combined with allowCredentials(true).
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**")
.allowedOrigins("https://app.example.com", "http://localhost:3000")
.allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS")
.allowedHeaders("Authorization", "Content-Type", "X-API-Key")
.exposedHeaders("Location", "X-Request-Id")
.maxAge(600);
}
}Fix it on a Node.js Express API
The cors middleware handles both simple and preflight requests. Pass the origins you trust; reflecting every origin while allowing credentials would let any website act on behalf of your logged-in users.
import cors from "cors";
app.use(cors({
origin: ["https://app.example.com", "http://localhost:3000"],
methods: ["GET", "POST", "PUT", "PATCH", "DELETE"],
allowedHeaders: ["Authorization", "Content-Type"],
exposedHeaders: ["Location"],
maxAge: 600,
}));When you cannot change the server
If the API is not yours and does not allow browser access, call it from a server or a terminal instead: run the cURL command directly, or convert it to Java, Python, Node.js or Go with the executor's Code tab and run that. Avoid public CORS proxies for anything with credentials, since they can read every request and response that passes through them.
Reference: MDN Web Docs: Cross-Origin Resource Sharing (CORS).