Testing CORS: why your API client works and the browser doesn't
It's one of the most common API support threads: "The request works in my API client, but the browser says blocked by CORS policy." Both are true. The API is fine, and the browser is refusing to let your frontend read the response.
Here's why that happens, and how to test CORS from your API client so the frontend never sees that error.
Why your API client "works"
CORS (Cross-Origin Resource Sharing) is a browser security rule. When JavaScript on https://app.example.com calls https://api.example.com, the browser sends the request, but only hands the response to the page if the API says that origin is allowed.
An API client is not a web page, so there's no page to protect:
- The Requestarium desktop app, the CLI and the IDE extensions send requests directly from your computer, with no CORS check.
- The web app sends requests through the Requestarium server, which doesn't apply CORS either.
So a request that works in your API client tells you the API answers. It doesn't tell you whether a browser will accept the answer. To know that, you have to look at the headers yourself.
What the browser checks
For a simple GET, the browser adds an Origin header, and checks that the response allows it:
GET /orders HTTP/1.1
Host: api.example.com
Origin: https://app.example.comHTTP/1.1 200 OK
Access-Control-Allow-Origin: https://app.example.com
Vary: OriginFor anything else (a JSON body, an Authorization header, PUT or DELETE), the browser first sends a preflight OPTIONS request and only sends the real one if the answer allows the method and headers:
OPTIONS /orders HTTP/1.1
Origin: https://app.example.com
Access-Control-Request-Method: POST
Access-Control-Request-Headers: authorization, content-typeHTTP/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: 600Test the response headers
Add an Origin header to a request, with the origin your frontend runs on, and check what comes back:
GET {{baseUrl}}/orders
Origin: {{frontendOrigin}}
Authorization: Bearer {{token}}pm.test('allows the frontend origin', () => {
pm.expect(pm.response.headers.get('Access-Control-Allow-Origin'))
.to.be.oneOf([pm.environment.get('frontendOrigin'), '*']);
});
pm.test('varies by origin when it echoes one', () => {
if (pm.response.headers.get('Access-Control-Allow-Origin') === '*') return;
pm.expect(pm.response.headers.get('Vary') || '').to.include('Origin');
});Put frontendOrigin in each environment: http://localhost:5173 for Local, https://staging-app.example.com for Staging, and so on. The same request then checks the right origin per stage. See How to set up API environments.
Test that other origins are refused
An API that answers Access-Control-Allow-Origin: * together with Access-Control-Allow-Credentials: true (or echoes back any origin it's sent) lets any website read your users' data. Test a hostile origin:
GET {{baseUrl}}/me
Origin: https://evil.example
Authorization: Bearer {{token}}pm.test('does not allow unknown origins', () => {
const allowed = pm.response.headers.get('Access-Control-Allow-Origin');
pm.expect(allowed).to.not.equal('https://evil.example');
});Test the preflight
Collections send GET, POST, PUT, PATCH and DELETE, so check the preflight from a terminal with curl:
curl -s -o /dev/null -D - -X OPTIONS "https://api.example.com/orders" \
-H "Origin: https://app.example.com" \
-H "Access-Control-Request-Method: POST" \
-H "Access-Control-Request-Headers: authorization, content-type"Look for a 2xx status and the three Access-Control-Allow-* headers. The most common preflight bugs:
401on the preflight. Preflights never carry your auth header, so the auth middleware must letOPTIONSthrough.- A header missing from
Allow-Headers, usually a custom one likeX-Request-IdorIdempotency-Key. - A redirect. Browsers don't follow redirects on preflights.
http→httpsor a missing trailing slash breaks them.
When the browser still complains
If your tests pass but the browser doesn't, open DevTools → Network, find the failed request (and the OPTIONS before it), and compare its headers with what you tested. Then save the session as HAR and import it to replay exactly what the browser sent.