QA
Beginner LevelREST APIs & Web Services

What is Cross-Origin Resource Sharing (CORS) and how do preflight OPTIONS requests work?

❓ Interview Prompt: Explain CORS browser security model, Same-Origin Policy, preflight `OPTIONS` requests, and response headers (`Access-Control-Allow-Origin`).

CORS is a browser security mechanism that restricts web applications running at one origin (domain/protocol/port) from making API requests to a different origin:

Simple vs Preflight Requests:

•
Simple Requests ('GET', 'HEAD', 'POST' with standard headers): Sent directly; browser verifies 'Access-Control-Allow-Origin' response header.
•
Preflight Requests: For requests using non-standard headers ('Authorization'), custom methods ('PUT'/'DELETE'), or 'application/json', the browser sends an automatic 'OPTIONS' preflight request first to check server permissions!

Code Implementation Example

http
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers: Content-Type, Authorization
Access-Control-Max-Age: 86400
💡Preflight OPTIONS HTTP response headers granting cross-origin permissions.
🎯 Interview Key Takeaway

CORS is enforced by browsers; preflight OPTIONS requests verify server cross-origin permission headers before sending actual requests.

#CORS#Security#Preflight#Beginner
Back to all REST APIs & Web Services Questions→

More REST APIs & Web Services Interview Questions

Beginner LevelREST APIs & Web Services

What is HTTP Method Idempotency and how do you implement Idempotency Keys for REST APIs?

Beginner LevelREST APIs & Web Services

What are standard HTTP Status Codes (200, 201, 204, 400, 401, 403, 404, 409, 429, 500, 503)?

Beginner LevelREST APIs & Web Services

What are HTTP Verbs (GET, POST, PUT, PATCH, DELETE) and when to use PUT vs PATCH?