Skip to content
tezvyn:

When does fetch trigger a CORS preflight, and what POST is complex?

Source: developer.mozilla.orgHardHow cards are made

When does fetch trigger a CORS preflight, and what POST is complex?

Tests whether you know the simple-request boundary. A strong answer names the three safe POST content-types and gives a cross-origin POST with application/json plus a custom header like Authorization.

What's really being asked

Whether you know the exact conditions that promote a cross-origin fetch from a simple request to a complex one requiring an OPTIONS preflight. Interviewers care because misconfigured CORS is a common production outage, and senior engineers should know the difference between a same-origin policy restriction and a preflight handshake. The question also checks if you can articulate a concrete counter-example rather than only reciting abstract rules, and whether you understand that the browser not the server initiates the OPTIONS check.

The full answer

First, state that preflights only happen on cross-origin requests because same-origin fetches never send OPTIONS for CORS. Second, explain that a request is simple when it uses GET, HEAD, or POST with only CORS-safelisted request headers and a body whose Content-Type is one of text/plain, application/x-www-form-urlencoded, or multipart/form-data. Third, note that any deviation triggers preflight, such as methods like PUT or DELETE, custom headers such as Authorization or X-API-Version, or a POST with Content-Type application/json. Fourth, describe the preflight mechanism itself: the browser sends an OPTIONS request carrying Access-Control-Request-Method and Access-Control-Request-Headers so the server can respond with appropriate Access-Control-Allow headers before the real request is attempted.

The mistakes people make

Saying that all POST requests require a preflight, or that preflights are sent for same-origin requests. Another red flag is blaming CORS entirely on the server without acknowledging that the browser enforces the policy; the server does not initiate the OPTIONS request, the browser does. Claiming that including cookies or credentials always forces a preflight is also incorrect; credentials affect whether the browser blocks the response, not whether an OPTIONS request occurs. Finally, confusing the preflight OPTIONS call with the actual request method is a signal of weak HTTP fundamentals.

What usually comes next

How would you handle a preflight failure in a production API? What is the impact of custom headers on caching? Can you avoid a preflight for a JSON POST, and should you? How do redirects interact with preflighted requests? What happens if the preflight response is cached by an intermediary?

A concrete example

Imagine a React app on https://app.example.com calling fetch with method POST, url https://api.other.com/events, headers including Content-Type application/json and Authorization Bearer token, and body JSON.stringify({event: click}). Because the Content-Type is not one of the three simple values and Authorization is a custom header, the browser first sends OPTIONS /events with Access-Control-Request-Method: POST and Access-Control-Request-Headers: content-type, authorization. Only if the server returns 204 with Access-Control-Allow-Origin: https://app.example.com and Access-Control-Allow-Headers: content-type, authorization does the browser proceed with the actual POST.

Interview question

A React app on https://app.example.com sends a cross-origin POST to https://api.other.com/events with Content-Type application/json and an Authorization header. Why does the browser first send an OPTIONS request?

  • a.The server requires authentication and must verify the token before accepting the POST.
  • b.The combination of application/json Content-Type and the Authorization header makes it a complex cross-origin request.Correct
  • c.POST requests with a request body always trigger a CORS preflight regardless of headers used.
  • d.Including credentials in a cross-origin request always forces the browser to perform a preflight check.
Why?

The correct answer reflects that application/json is not a CORS-safelisted Content-Type and Authorization is a custom header, so the browser must preflight the complex cross-origin request. The most tempting distractor incorrectly suggests the server initiates the OPTIONS call to verify auth, whereas the card states the browser, not the server, enforces CORS by sending the preflight.

Just read this? Test yourself on what you have been reading.

Read the original → developer.mozilla.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles