API Fundamentals Course
API Fundamentals
/
Advanced

OPTIONS

Definition

An HTTP method used to describe the communication options (methods, headers) available for the target resource, often used automatically by browsers for CORS preflight requests.

Explain Like I'm New

Before your browser sends a massive POST request with sensitive data to a different website, it gets paranoid. It sends a tiny, invisible OPTIONS request first, asking: 'Hey server, are you allowed to accept POST requests from me?' The server replies 'Yes'. Then the browser sends the real POST request.

Real World Example

Cross-Origin Resource Sharing (CORS). If your frontend is on `localhost:3000` and your backend is on `api.com`, every POST, PUT, and DELETE request will secretly trigger an OPTIONS 'Preflight' request first.

Common Use Cases

  • •CORS Preflight
  • •Discovering API capabilities

Terminal Output

bash / terminal
/* The Invisible CORS Handshake (Preflight) */ // 1. Browser secretly sends OPTIONS first: OPTIONS /api/data HTTP/1.1 Origin: http://localhost:3000 Access-Control-Request-Method: PUT // 2. Server responds: HTTP/1.1 204 No Content Access-Control-Allow-Origin: http://localhost:3000 Access-Control-Allow-Methods: GET, POST, PUT, DELETE Access-Control-Allow-Headers: Content-Type, Authorization // 3. Browser sees it is allowed, and NOW sends the real PUT request.

Interview Questions

basic

  • Do developers usually have to write frontend code to send OPTIONS requests manually?

intermediate

  • If a server receives an OPTIONS request, what specific headers must it return to allow CORS?

Flash Cards

Question

Manual code?

Click to reveal answer
Answer

No. The web browser (Chrome, Firefox) generates and sends OPTIONS requests completely automatically under the hood when making Cross-Origin requests.

Question

Which headers?

Click to reveal answer
Answer

It must return `Access-Control-Allow-Origin` (who is allowed), `Access-Control-Allow-Methods` (what verbs are allowed), and `Access-Control-Allow-Headers`.