Junior — Middle
What is the purpose of the HTTP request OPTIONS and when is it used?
sobes.tech AI
Answer from AI
The HTTP OPTIONS request is used to determine which HTTP methods the server or a specific resource supports. It allows the client to find out which operations (GET, POST, PUT, DELETE, etc.) are permitted without executing the operation itself.
Most commonly, OPTIONS is used in the following cases:
- When implementing CORS (Cross-Origin Resource Sharing), browsers send a preflight OPTIONS request to check if the request from another domain is allowed.
- For API diagnostics and testing to find out supported methods.
Example of an OPTIONS request:
OPTIONS /api/resource HTTP/1.1
Host: example.com
The server's response usually contains an Allow header with a list of permitted methods:
HTTP/1.1 200 OK
Allow: GET, POST, OPTIONS