# Preflight Requests

> A preliminary OPTIONS request sent automatically by the browser to check if a server permits the actual cross-origin request.

Subject: Internet | Lesson 19 of 35 | 3 min read
Interactive lesson: https://codewithanimation.in/internet/preflight-requests
Previous: [CORS](https://codewithanimation.in/internet/cors.md) | Next: [Web Hosting Concepts](https://codewithanimation.in/internet/web-hosting-concepts.md)

## What it is

A CORS Preflight Request is a preliminary request sent automatically by the browser to check if a server understands and permits the actual cross-origin request.

It uses the HTTP `OPTIONS` method and includes specific headers like `Access-Control-Request-Method` and `Access-Control-Request-Headers` to describe the upcoming request to the server.

## Why it matters

Preflight requests matter because they introduce an extra network round-trip, which can add significant **latency** to web applications.

Front-end developers must understand the distinction between 'simple requests' (which don't trigger a preflight) and 'complex requests' (which do), so they can optimize performance. A key optimization is ensuring the server caches preflight permissions via the `Access-Control-Max-Age` header.

## How it works

When a browser executes a 'complex' cross-origin request (e.g., sending JSON or using custom headers), it intercepts the request.

It automatically sends an HTTP `OPTIONS` request (the preflight) to the server's endpoint.

The server inspects the requested method and headers, then responds with `Access-Control-Allow-Methods`, `Access-Control-Allow-Headers`, etc., indicating what is permitted.

If the server grants permission, the browser instantly sends the actual `POST`/`PUT`/`DELETE` request. If denied, the browser blocks the actual request and throws a CORS error.

## Common mistake: Common mistake

**The Double Request Confusion**: The #1 misconception for beginners is looking at the DevTools Network tab and seeing two entries for a single `fetch()` call. They mistakenly believe their code is buggy and firing twice, wasting time debugging their front-end logic instead of realizing the first request is just the browser's automatic preflight `OPTIONS` check.

## Tip: Pro tip

Think of a preflight request like the **Chainsaw Permission Model**.

You don't just walk into a neighbor's yard and start up a chainsaw (the actual request). First, you send a text asking, 'Hey, can I come over and use a chainsaw?' (the `OPTIONS` preflight). Only when they reply 'Yes, chainsaws are allowed' do you proceed.

## Try it: Try it

Play the **Browser Security Guard Minigame**. Send a complex POST request, watch the Scout (OPTIONS) get permission, and configure the server to let the actual request through!

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/internet/preflight-requests

## Check yourself

### Question 1

Which HTTP method is automatically used by the browser to send a preflight request?

- A. GET
- B. CORS
- C. OPTIONS
- D. HEAD

Correct answer: C

The browser automatically sends an OPTIONS request to check the server's CORS permissions before sending the actual complex request.

- Why not A: GET is used for fetching resources, not for preflight permission checks.
- Why not B: CORS is a security policy/mechanism, not an HTTP method.
- Why not D: HEAD fetches the headers of a resource, but doesn't check cross-origin permissions.

### Question 2

Which of the following makes a request 'complex' and triggers a preflight request?

- A. Using a standard GET method with no extra headers.
- B. Setting the Content-Type to application/x-www-form-urlencoded.
- C. Making an API request to the exact same origin.
- D. Including a custom header like Authorization: Bearer <token>.

Correct answer: D

Adding custom headers (like Authorization) or non-standard Content-Types (like application/json) automatically upgrades a request to 'complex', requiring a preflight request.

- Why not A: This qualifies as a 'simple request' and bypasses the preflight check.
- Why not B: This is a standard form content type and is treated as a simple request.
- Why not C: Same-origin requests never trigger CORS policies or preflight checks.

### Question 3

How can a developer reduce the latency caused by preflight requests?

- A. Wrap the fetch call in a setTimeout.
- B. Change all API requests to use the OPTIONS method.
- C. Have the server send an Access-Control-Max-Age header in the preflight response.
- D. Disable CORS in the browser settings.

Correct answer: C

The Access-Control-Max-Age header tells the browser to cache the preflight response permissions for a specified time, bypassing the extra OPTIONS round-trip for subsequent requests.

- Why not A: This just delays the request further without eliminating the preflight round-trip.
- Why not B: This breaks standard REST patterns and won't fetch actual data.
- Why not D: This is a security risk and only applies to the developer's local browser, not end users.

## Remember this

- Preflights are automatic OPTIONS requests sent by the browser.
- They are triggered by 'complex' requests (custom headers, JSON bodies).
- They add latency, but can be cached via Access-Control-Max-Age.
