HEAD and OPTIONS methods in Rest API - Time & Space Complexity
We want to understand how the time to handle HEAD and OPTIONS requests changes as the number of resources grows.
How does the server's work grow when it processes these methods?
Analyze the time complexity of the following REST API handling code.
// Example pseudocode for OPTIONS method
function handleOptions(request) {
let allowedMethods = ["GET", "POST", "HEAD", "OPTIONS"];
return { "Allow": allowedMethods.join(", ") };
}
// Example pseudocode for HEAD method
function handleHead(request, resource) {
let headers = getHeaders(resource);
return { headers };
}
This code returns allowed methods for OPTIONS and headers only for HEAD without body.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Joining the list of allowed methods for OPTIONS.
- How many times: The join operation runs once per OPTIONS request, over a fixed small list.
- HEAD method: Fetching headers is a direct lookup, no loops over data.
The allowed methods list is small and fixed, so the work for OPTIONS stays the same no matter how many resources exist.
Fetching headers for HEAD depends on the resource but usually is a direct access, so it grows very little with resource size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Few operations (joining 4 methods, fetching headers) |
| 100 | Same few operations, no increase |
| 1000 | Still same few operations, no increase |
Pattern observation: The work stays constant regardless of input size.
Time Complexity: O(1)
This means the time to handle HEAD or OPTIONS requests stays the same no matter how many resources the server has.
[X] Wrong: "Handling OPTIONS or HEAD requests takes longer as the number of resources grows."
[OK] Correct: These methods usually only check fixed information or headers, so their work does not grow with resource count.
Understanding that some HTTP methods have constant time handling helps you explain efficient API design and server behavior clearly.
"What if the OPTIONS method dynamically generated allowed methods based on resource state? How would the time complexity change?"
Practice
HEAD method in REST APIs?Solution
Step 1: Understand the
TheHEADmethod roleHEADmethod requests headers only, not the full content.Step 2: Compare with other methods
UnlikeGET, it does not return the body, only metadata.Final Answer:
To retrieve only the headers of a resource without the body -> Option DQuick Check:
HEADreturns headers only [OK]
- Confusing HEAD with GET which returns full content
- Thinking HEAD modifies data on server
- Assuming HEAD returns body content
OPTIONS request using curl?Solution
Step 1: Identify the method flag in curl
The-Xoption specifies the HTTP method to use.Step 2: Match the method to the question
To send anOPTIONSrequest, use-X OPTIONS.Final Answer:
curl -X OPTIONS https://api.example.com/resource -> Option CQuick Check:
curl -X OPTIONS sends OPTIONS request [OK]
- Using -X HEAD instead of OPTIONS
- Using --get which sends GET request
- Using POST method incorrectly
OPTIONS request:Allow: GET, POST, OPTIONS, HEAD
What does this header tell the client?
Solution
Step 1: Read the Allow header
TheAllowheader lists HTTP methods the server supports for the resource.Step 2: Interpret the methods listed
It shows GET, POST, OPTIONS, and HEAD are allowed methods.Final Answer:
The server supports GET, POST, OPTIONS, and HEAD methods on this resource -> Option BQuick Check:
Allow header lists supported methods [OK]
- Ignoring OPTIONS and HEAD in the list
- Assuming only GET and POST are allowed
- Confusing Allow header with authentication
HEAD requests in your REST API server:if request.method == 'HEAD':
return get_resource()But clients receive the full content body. What is the problem?
Solution
Step 1: Analyze the code behavior
The code callsget_resource()which returns full content including body.Step 2: Understand HEAD response requirements
HEAD must return headers only, no body content.Final Answer:
The code returns full content instead of headers only -> Option AQuick Check:
HEAD returns headers only, not full body [OK]
- Returning full content for HEAD requests
- Using wrong method name case
- Confusing HEAD with GET behavior
HEAD and OPTIONS methods. Which approach best improves client efficiency?Solution
Step 1: Understand
HEADusageHEADchecks if a resource exists by fetching headers without body, saving bandwidth.Step 2: Understand
OPTIONSusageOPTIONStells clients which HTTP methods are allowed, helping avoid invalid requests.Step 3: Combine for efficiency
Using both before heavy requests reduces unnecessary data transfer and errors.Final Answer:
UseHEADto check resource existence andOPTIONSto discover allowed methods before sending requests -> Option AQuick Check:
HEAD checks headers, OPTIONS lists methods [OK]
- Using OPTIONS to fetch full data (wrong)
- Using HEAD to modify resources (wrong)
- Ignoring these methods and wasting bandwidth
Ready to prove it?
Pass the test and this pattern moves unknown
