What is a CORS preflight request, and when does the browser automatically send one before the actual request?
Short answer
For 'non-simple' requests (using methods other than GET/HEAD/POST, custom headers, or a Content-Type outside a few basic ones like application/x-www-form-urlencoded), the browser sends an OPTIONS preflight request first, asking the server whether it's willing to accept the…
Advertisement
AModel answer
For 'non-simple' requests (using methods other than GET/HEAD/POST, custom headers, or a Content-Type outside a few basic ones like application/x-www-form-urlencoded), the browser sends an OPTIONS preflight request first, asking the server whether it's willing to accept the actual cross-origin request via Access-Control-Request-Method and Access-Control-Request-Headers headers -- only if the server responds with matching Access-Control-Allow-* headers does the browser proceed to send the real request.
Example
// Browser automatically sends BEFORE a PUT request with a custom header: // OPTIONS /api/products/5 // Access-Control-Request-Method: PUT // Access-Control-Request-Headers: X-Custom-Header // Server must respond with matching Access-Control-Allow-Methods/Headers
Real-world example
A team debugging why PUT requests with a custom Authorization-like header mysteriously fail cross-origin discovers the API never explicitly allowed that custom header in its CORS policy, causing the preflight OPTIONS request to fail silently before the real request is ever sent.
Likely follow-ups
- What HTTP methods/headers count as 'simple' and avoid triggering a preflight?
- How can preflight results be cached client-side to avoid repeating them on every request?
Related topics