What if your computer could instantly tell you exactly what happened with every request you make?
Why status codes communicate outcomes in Rest API - The Real Reasons
Imagine you send a letter asking for information, but the reply is just a blank page or a confusing note. You don't know if your request was received, if it succeeded, or if there was a problem.
Without clear signals, you waste time guessing what happened. You might retry unnecessarily, miss errors, or misunderstand the response. This slows down communication and causes frustration.
Status codes act like clear signals or traffic lights. They quickly tell you if your request was successful, if you need to try again, or if something went wrong. This makes communication fast and reliable.
Response: "Here is your data." // No clue if success or error
Status: 200 OK Response: "Here is your data."
Clear status codes let programs and people understand outcomes instantly, making systems smarter and more efficient.
When you log into a website, a status code tells your browser if login worked or if your password was wrong, so you get the right message immediately.
Status codes give quick, clear feedback on requests.
They prevent confusion and save time.
They help build reliable and user-friendly systems.
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
