HTTP OPTIONS Method

Die HTTP-Methode OPTIONS fragt die unterstützten HTTP-Methoden und Kommunikations-Optionen für eine Ressource ab. Sie wird primär für CORS-Preflight-Requests verwendet und liefert erlaubte Methoden via Allow-Header für API-Introspection.

Typ

HTTP-Methode

Syntax

OPTIONS-Request mit Ressourcen-URI oder Asterisk für Server-weite Optionen.

http
OPTIONS /api/users HTTP/1.1
Host: api.example.com

Direktiven

Die Direktiven definieren OPTIONS-Semantik und Response-Header.

Allow
Response-Header listet unterstützte HTTP-Methoden für die Ressource.
Access-Control-Allow-Methods
CORS-Header listet erlaubte Methoden für Cross-Origin-Requests.
Access-Control-Allow-Headers
CORS-Header listet erlaubte Custom-Header für CORS-Requests.
Access-Control-Max-Age
CORS-Header definiert Preflight-Cache-Dauer in Sekunden.
* (Asterisk URI)`
OPTIONS * fragt Server-weite Optionen ab statt ressourcen-spezifische.

Beispiele

Nachfolgend finden Sie praktische Anwendungsbeispiele für die OPTIONS-Methode.

Beispiel 1 CORS Preflight Request

http
OPTIONS /api/users HTTP/1.1
Host: api.example.com
Origin: https://app.example.com
Access-Control-Request-Method: DELETE
Access-Control-Request-Headers: Authorization

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: Authorization, Content-Type
Access-Control-Max-Age: 86400

Browser sendet Preflight vor DELETE-Request, Server erlaubt Cross-Origin DELETE mit Authorization-Header.

Beispiel 2 API Capabilities Discovery

http
OPTIONS /api/articles/42 HTTP/1.1
Host: blog.example.com

HTTP/1.1 200 OK
Allow: GET, HEAD, PUT, DELETE, OPTIONS
Content-Type: application/json

{
  "methods": ["GET", "PUT", "DELETE"],
  "formats": ["application/json", "application/xml"]
}

Client fragt erlaubte Operationen ab, Server listet verfügbare Methoden und Formate.

Beispiel 3 Server-Wide Options

http
OPTIONS * HTTP/1.1
Host: api.example.com

HTTP/1.1 200 OK
Allow: GET, POST, PUT, DELETE, HEAD, OPTIONS
Server: nginx/1.21.6

Client fragt Server-weite Capabilities ab mit Asterisk-URI.

CORS Preflight Flow

OPTIONS CORS-Preflight-Ablauf

Vorteile für die Systemarchitektur

  • CORS-Security: Browser prüft erlaubte Cross-Origin-Operationen vor tatsächlichem Request
  • API-Discovery: Clients können unterstützte Methoden dynamisch ermitteln für generische API-Clients
  • Preflight-Caching: Access-Control-Max-Age reduziert Preflight-Requests für wiederkehrende Operationen

Spezifikation

RFC 9110, Section 9.3.7 – HTTP Semantics https://www.rfc-editor.org/rfc/rfc9110.html#name-options WHATWG Fetch Standard – CORS Protocol https://fetch.spec.whatwg.org/#http-cors-protocol

Weitere Spezifikationen

Allow Header, Access-Control-Allow-Methods Header, Access-Control-Allow-Origin Header