Why status codes communicate outcomes in Rest API - Performance Analysis
When a REST API sends a status code, it tells us what happened with the request.
We want to understand how the time to process and send these codes changes as the size of the request grows.
Analyze the time complexity of this simple REST API response code snippet.
def handle_request(request):
if request.is_valid():
return 200 # OK
else:
return 400 # Bad Request
This code checks if a request is valid and returns a status code accordingly.
Look for loops or repeated checks in the code.
- Primary operation: Single validation check on the request.
- How many times: Exactly once per request.
The time to check and return a status code stays about the same no matter the size of the request.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 check and return |
| 100 | 1 check and return |
| 1000 | 1 check and return |
Pattern observation: Each request is handled independently with a fixed number of steps.
Time Complexity: O(1)
This means the time to decide and send a status code does not grow with the size of the request.
[X] Wrong: "Returning a status code takes longer if the request is bigger."
[OK] Correct: The status code is just a short message sent after a quick check, so its time does not depend on request size.
Understanding how status codes work and their time cost helps you explain API behavior clearly and confidently.
"What if the validation involved checking every item in a large list inside the request? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of status codes
Status codes are 3-digit numbers sent by the server to tell the client if the request was successful or if there was an error.Step 2: Identify the correct purpose
They do not style content, store data, or encrypt data; their main job is to communicate the outcome of the request.Final Answer:
To communicate the outcome of a server request -> Option BQuick Check:
Status codes = communicate outcome [OK]
- Thinking status codes style pages
- Confusing status codes with data storage
- Assuming status codes encrypt data
Solution
Step 1: Recall correct method to set status code
In many REST frameworks, the method to set status code isstatus(), notcode()orstatusCode().Step 2: Match status code and message
404 means 'Not Found', soresponse.status(404).send('Not Found')is correct syntax.Final Answer:
response.status(404).send('Not Found') -> Option AQuick Check:
Use status() to set code [OK]
- Using response.code() instead of response.status()
- Sending wrong status code for error
- Mixing status code and message methods
if (userFound) {
res.status(200).json({ message: 'User found' });
} else {
res.status(404).json({ error: 'User not found' });
}What will be the HTTP status code if the user is not found?
Solution
Step 1: Analyze the condition when user is not found
IfuserFoundis false, the else block runs, sending status 404.Step 2: Identify the status code sent in else block
The else block usesres.status(404), which means 'Not Found'.Final Answer:
404 -> Option CQuick Check:
User missing = 404 status [OK]
- Confusing 404 with 200 success
- Assuming 500 server error instead
- Mixing 400 bad request with 404
res.sendStatus(201).json({ message: 'Created' });What is the error and how to fix it?
Solution
Step 1: Understand sendStatus behavior
sendStatus(201)sends the status and ends the response immediately, so chaining.json()after it causes an error.Step 2: Correct way to send status with JSON
Useres.status(201).json(...)to set status and send JSON in one response.Final Answer:
sendStatus ends response; use res.status(201).json(...) instead -> Option DQuick Check:
sendStatus ends response; use status() + json() [OK]
- Chaining json() after sendStatus()
- Using send(201) instead of status(201)
- Changing status code instead of fixing method
Solution
Step 1: Understand 204 status meaning
204 means 'No Content', so the response must not include a body.Step 2: Choose code that sends 204 with no body
res.status(204).send();sends status 204 with empty body, which is correct. Sending JSON or message body breaks 204 rules.Final Answer:
res.status(204).send(); -> Option AQuick Check:
204 = no content, so send empty response [OK]
- Sending JSON body with 204 status
- Using 200 instead of 204 for delete success
- Chaining json() after sendStatus(204)
Ready to prove it?
Pass the test and this pattern moves unknown
