Security
What is CORS?
Definition
CORS is a browser mechanism that lets a server use HTTP response headers to declare which other origins are allowed to read its responses.
Also known as: Cross-Origin Resource Sharing
CORS in more detail
Browsers enforce the same-origin policy: script on one origin — scheme, host and port together — cannot read responses from another. CORS is the controlled exception. The server includes Access-Control-Allow-Origin and related headers, and the browser uses them to decide whether to hand the response body to the calling JavaScript. Understanding the direction matters: CORS relaxes a browser restriction, it does not add a restriction to your server.
For anything beyond a simple GET or form-style POST, the browser sends a preflight first: an OPTIONS request announcing the method and headers it intends to use. The server must answer that OPTIONS with the allowed origin, methods and headers, and may set Access-Control-Max-Age so the answer is cached. If the real request needs cookies or an Authorization header, the client must set credentials mode and the server must return Access-Control-Allow-Credentials: true — and in that case the wildcard origin is forbidden, so the exact origin has to be echoed back.
Two consequences follow. First, a CORS error is not a server error: the request usually reached your API and was answered, and the browser then refused to expose the response, which is why curl and Postman work when the browser does not. Second, CORS is not access control. Anything that is not a browser ignores it entirely, so a public endpoint with a permissive CORS policy is still public — authentication and authorization are what protect data.
CORS example
Simple request
Origin: https://app.example.com
->
Access-Control-Allow-Origin: https://app.example.com
Preflight, triggered by a custom header or PUT/DELETE/PATCH
OPTIONS /v1/orders/12
Origin: https://app.example.com
Access-Control-Request-Method: PATCH
Access-Control-Request-Headers: content-type, authorization
->
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Methods: GET, PATCH, DELETE
Access-Control-Allow-Headers: content-type, authorization
Access-Control-Max-Age: 600
With cookies, the wildcard is illegal
Access-Control-Allow-Origin: * + credentials -> blocked
Echo the exact origin and add
Access-Control-Allow-Credentials: trueCORS — common questions
- Why am I getting a CORS error?
- Because the response did not carry an Access-Control-Allow-Origin header matching your page's origin, so the browser refused to expose it to your script. The fix belongs on the server or its proxy; nothing you change in the client can override it.
- Does CORS protect my API?
- No. It only governs whether browser JavaScript from another origin may read responses. Any script, server or command-line tool can call the endpoint directly and ignore CORS entirely, so real protection comes from authentication, authorization and rate limiting.
- What triggers a preflight request?
- Any method other than GET, HEAD or POST; a Content-Type other than the three form-style ones; or custom request headers such as Authorization or X-Request-Id. Those combinations are not considered simple requests, so the browser asks permission first.
Keep reading
Every definition in the set is indexed A-Z in the developer glossary, and the cheat sheets cover the syntax once you know what you are looking for.
Browse the A-Z glossary