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?
Think about a status code that means success but no response body.
204 No Content means the request was successful but the server has no content to return. It is commonly used for DELETE requests.
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?
Think about how clients handle response bodies.
204 No Content tells clients explicitly that there is no response body, so clients can avoid unnecessary parsing or rendering.
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?
app.delete('/item/:id', (req, res) => { // Assume item is deleted successfully res.status(204).send(); });
Check what res.status(204).send() does.
Setting status 204 and calling send() sends a response with status 204 and no content in the body.
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?
fetch('/api/delete/123', { method: 'DELETE' }) .then(res => res.json()) .then(data => console.log(data)) .catch(err => console.error('Error:', err));
Think about parsing empty response as JSON.
Calling res.json() on an empty response body causes a SyntaxError because there is no JSON to parse.
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?
Check how to avoid parsing JSON when no content is returned.
Option C checks for 204 status and returns null instead of calling res.json(), avoiding parsing errors. Then it updates UI accordingly.