Bird
Raised Fist0
Rest APIprogramming~10 mins

Why status codes communicate outcomes in Rest API - Visual Breakdown

Choose your learning style10 modes available
Concept Flow - Why status codes communicate outcomes
Client sends request
↓
Server processes request
↓
Server selects status code
↓
Server sends response with status code
↓
Client reads status code
↓
Client decides next action based on code
The client sends a request, the server processes it and picks a status code to show the outcome, then the client reads this code to know what happened.
Execution Sample
Rest API
GET /api/data HTTP/1.1
Host: example.com

HTTP/1.1 404 Not Found

{}
Client requests data; server responds with 404 status code meaning the data was not found.
Execution Table
StepActionStatus Code ChosenMeaningClient Reaction
1Client sends GET requestN/AN/AWaiting for response
2Server checks if data existsN/AN/AN/A
3Data not found, server selects 404404Resource not foundPrepare to handle missing data
4Server sends response with 404404Resource not foundReceives 404, shows error or fallback
5Client decides next stepN/AN/AMay retry, show message, or stop
💡 Process ends after client receives and reacts to status code.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
status_codeNoneNone404404404
client_stateWaitingWaitingWaitingReceived 404Handled 404
Key Moments - 2 Insights
Why does the server send a status code instead of just data?
The status code quickly tells the client what happened (success, error, etc.) without needing to read the whole response. See execution_table step 3 and 4.
What does a 404 status code mean for the client?
It means the requested resource was not found, so the client should handle this case, like showing an error message. See execution_table step 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what status code does the server send when data is missing?
A500
B200
C404
D301
💡 Hint
Check the 'Status Code Chosen' column at step 3 and 4.
At which step does the client receive the status code?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Client Reaction' column to see when the client gets the code.
If the server found the data successfully, which status code would you expect instead of 404?
A404
B200
C500
D403
💡 Hint
200 means success; see common HTTP status codes.
Concept Snapshot
Status codes are numbers servers send to show request results.
They tell if request succeeded (200), failed (404), or errored (500).
Clients read these codes to decide what to do next.
This communication is fast and clear without extra data.
Full Transcript
When a client sends a request to a server, the server processes it and picks a status code to communicate the outcome. For example, if the requested data is missing, the server sends a 404 status code meaning 'Not Found'. The client reads this code to know what happened and decides how to respond, like showing an error message or retrying. Status codes are a simple way for servers to tell clients about success, errors, or other results quickly and clearly.

Practice

(1/5)
1. What is the main purpose of HTTP status codes in REST APIs?
easy
A. To style the webpage content
B. To communicate the outcome of a server request
C. To store user data on the server
D. To encrypt the data sent between client and server

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    To communicate the outcome of a server request -> Option B
  4. Quick Check:

    Status codes = communicate outcome [OK]
Hint: Status codes show if request worked or failed [OK]
Common Mistakes:
  • Thinking status codes style pages
  • Confusing status codes with data storage
  • Assuming status codes encrypt data
2. Which of the following is the correct syntax to send a 404 status code in a REST API response?
easy
A. response.status(404).send('Not Found')
B. response.code(404).send('Not Found')
C. response.sendStatus(200)
D. response.statusCode(500).send('Error')

Solution

  1. Step 1: Recall correct method to set status code

    In many REST frameworks, the method to set status code is status(), not code() or statusCode().
  2. Step 2: Match status code and message

    404 means 'Not Found', so response.status(404).send('Not Found') is correct syntax.
  3. Final Answer:

    response.status(404).send('Not Found') -> Option A
  4. Quick Check:

    Use status() to set code [OK]
Hint: Use response.status(code) to set HTTP status [OK]
Common Mistakes:
  • Using response.code() instead of response.status()
  • Sending wrong status code for error
  • Mixing status code and message methods
3. Consider this code snippet in a REST API handler:
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?
medium
A. 500
B. 200
C. 404
D. 400

Solution

  1. Step 1: Analyze the condition when user is not found

    If userFound is false, the else block runs, sending status 404.
  2. Step 2: Identify the status code sent in else block

    The else block uses res.status(404), which means 'Not Found'.
  3. Final Answer:

    404 -> Option C
  4. Quick Check:

    User missing = 404 status [OK]
Hint: 404 means resource not found [OK]
Common Mistakes:
  • Confusing 404 with 200 success
  • Assuming 500 server error instead
  • Mixing 400 bad request with 404
4. This REST API code tries to send a 201 status but has a bug:
res.sendStatus(201).json({ message: 'Created' });

What is the error and how to fix it?
medium
A. Change 201 to 200 to fix the bug
B. Use res.send(201).json(...) to fix it
C. No error; code works fine
D. sendStatus ends response; use res.status(201).json(...) instead

Solution

  1. Step 1: Understand sendStatus behavior

    sendStatus(201) sends the status and ends the response immediately, so chaining .json() after it causes an error.
  2. Step 2: Correct way to send status with JSON

    Use res.status(201).json(...) to set status and send JSON in one response.
  3. Final Answer:

    sendStatus ends response; use res.status(201).json(...) instead -> Option D
  4. Quick Check:

    sendStatus ends response; use status() + json() [OK]
Hint: sendStatus ends response; use status() before json() [OK]
Common Mistakes:
  • Chaining json() after sendStatus()
  • Using send(201) instead of status(201)
  • Changing status code instead of fixing method
5. You want your REST API to return a 204 status code when a DELETE request succeeds but with no content in the response body. Which code snippet correctly implements this?
hard
A. res.status(204).send();
B. res.status(204).json({ message: 'Deleted' });
C. res.sendStatus(204).json({ message: 'Deleted' });
D. res.status(200).send('Deleted');

Solution

  1. Step 1: Understand 204 status meaning

    204 means 'No Content', so the response must not include a body.
  2. 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.
  3. Final Answer:

    res.status(204).send(); -> Option A
  4. Quick Check:

    204 = no content, so send empty response [OK]
Hint: 204 means no content; send empty response [OK]
Common Mistakes:
  • Sending JSON body with 204 status
  • Using 200 instead of 204 for delete success
  • Chaining json() after sendStatus(204)
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard