0
0
Rest APIprogramming~20 mins

204 No Content in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
204 No Content Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:00remaining
What is the HTTP status code returned?

You send a DELETE request to remove a resource from a REST API. The server successfully deletes the resource but returns no content in the response body.

What HTTP status code should the server return?

A200 OK
B204 No Content
C404 Not Found
D500 Internal Server Error
Attempts:
2 left
💡 Hint

Think about a status code that means success but no response body.

🧠 Conceptual
intermediate
1:30remaining
Why use 204 No Content instead of 200 OK with empty body?

When a REST API successfully processes a request but has no content to return, why is 204 No Content preferred over 200 OK with an empty response body?

A200 OK causes the client to retry the request automatically.
B200 OK is only for GET requests, so it cannot be used here.
C204 No Content allows caching, 200 OK does not.
D204 No Content explicitly tells the client there is no body, so it can skip parsing.
Attempts:
2 left
💡 Hint

Think about how clients handle response bodies.

Predict Output
advanced
1:30remaining
What is the output of this Express.js code?

Consider this Node.js Express server code snippet:

app.delete('/item/:id', (req, res) => {
  // Assume item is deleted successfully
  res.status(204).send();
});

What will the client receive as the HTTP status and body?

Rest API
app.delete('/item/:id', (req, res) => {
  // Assume item is deleted successfully
  res.status(204).send();
});
AStatus 204 with empty body
BStatus 204 with JSON body {"message":"Deleted"}
CStatus 200 with empty body
DStatus 404 with error message
Attempts:
2 left
💡 Hint

Check what res.status(204).send() does.

🔧 Debug
advanced
2:00remaining
Why does this API client fail to handle 204 response?

This JavaScript fetch client code tries to parse JSON from a 204 No Content response:

fetch('/api/delete/123', { method: 'DELETE' })
  .then(res => res.json())
  .then(data => console.log(data))
  .catch(err => console.error('Error:', err));

What error will occur and why?

Rest API
fetch('/api/delete/123', { method: 'DELETE' })
  .then(res => res.json())
  .then(data => console.log(data))
  .catch(err => console.error('Error:', err));
ASyntaxError because response body is empty and cannot be parsed as JSON
BTypeError because fetch does not support DELETE method
CReferenceError because 'res' is undefined
DNo error, logs empty object
Attempts:
2 left
💡 Hint

Think about parsing empty response as JSON.

🚀 Application
expert
2:30remaining
How to correctly handle 204 No Content in a React app fetch call?

You make a DELETE request in a React app using fetch. The server returns 204 No Content. You want to update the UI after deletion without errors.

Which code snippet correctly handles the 204 response?

A
fetch('/api/item/1', { method: 'DELETE' })
  .then(res => {
    if (res.ok) return res.json();
  })
  .then(data => updateUI(data));
B
fetch('/api/item/1', { method: 'DELETE' })
  .then(res => res.json())
  .then(data => updateUI(data));
C
fetch('/api/item/1', { method: 'DELETE' })
  .then(res => {
    if (res.status === 204) return null;
    return res.json();
  })
  .then(data => {
    if (data !== null) updateUI(data);
    else updateUI(null);
  });
D
fetch('/api/item/1', { method: 'DELETE' })
  .then(res => res.text())
  .then(text => updateUI(text));
Attempts:
2 left
💡 Hint

Check how to avoid parsing JSON when no content is returned.