opyebCorsLab

When a CORS preflight (OPTIONS) is sent, and what the browser checks

Preflight · 7 min

In one line: The exact conditions that make the browser send OPTIONS before the real request, the three headers it carries, and the order in which the response is checked.

Half of all “the server returns 200 but the browser refuses” cases are decided at the preflight. Before sending a cross-origin request that might be unsafe, the browser sends OPTIONS to ask “will you allow this combination?”. Only if that answer is acceptable does the real request go out.

1. When a preflight is sent

If all three rows below hold, the request is a simple request and goes out without a preflight. If any one fails, a preflight is sent.

ItemSimple-request condition
MethodGET, HEAD, POST
Request headersAll of them are one of Accept, Accept-Language, Content-Language, Content-Type, Range
Content-Typeapplication/x-www-form-urlencoded, multipart/form-data, text/plain

Two things are missed most often. First, PUT, PATCH and DELETE always trigger a preflight. Second, Content-Type: application/json is not on that list, so a POST carrying JSON still gets a preflight. That is why CORS errors so often appear “only on the JSON API”.

2. What the preflight carries

OPTIONS /api/items HTTP/1.1
Origin: https://app.example.com
Access-Control-Request-Method: PUT
Access-Control-Request-Headers: authorization, content-type, x-api-key
  • Access-Control-Request-Method — the single method the real request will use.
  • Access-Control-Request-Headers — only the header names that are not safelisted, comma separated and lower-cased.
  • No cookies are attached to the preflight, even with credentials: include. The credentials rules still apply to the response, though.

3. What the response must satisfy

  1. Status — must be 2xx. A 301, 403, 404 or 500 is a failure by itself.
  2. Access-Control-Allow-Origin — the request origin exactly, or * when credentials are not used.
  3. Access-Control-Allow-Methods — must contain the requested method.
  4. Access-Control-Allow-Headers — must contain every name from Access-Control-Request-Headers; one missing name fails the whole request.
  5. Access-Control-Allow-Credentials — must be true when the mode is include.
  6. Access-Control-Max-Age — how long the passing preflight may be cached.

If the preflight passes, the browser sends the real request and checks Allow-Origin and Allow-Credentials again on that response. A correct preflight with a missing header on the actual response still fails.

4. Common misconceptions

  • “Just return 200 to OPTIONS” — the status is not enough. Allow-Methods and Allow-Headers must match the request.
  • “OPTIONS never reaches my server” — a CDN or proxy may answer or cache it. Check the edge configuration too.
  • “The browser preflights every time” — without Max-Age the result is cached for 5 seconds only. Around 600 seconds removes most round trips.

5. Checking it in CorsLab

Change the method to PUT or add content-type: application/json and the banner switches to “preflight required”, with the reason listed. Remove PUT from Allow-Methods and the block, plus the exact console text, appears.

Put the same request and response into the simulator to see whether it passes and which header to change.

Open CorsLab

Frequently asked questions

Can a GET request trigger a preflight?

Yes. A GET that carries Authorization or X-Custom is no longer a simple request and is preflighted. Judging by the method alone misses this case.

Does the preflight response need a body?

No. Only the status and headers matter. Answering with 204 No Content is common.

What shows up in the server log when a preflight fails?

One OPTIONS request and its response. The real request is never sent, so it looks as if the request never arrived.

Sources

Read next

0 bytes of your input sent to a server (advertising and affiliate widgets excluded)