The 404 Not Found status tells you that the web address you asked for does not exist on the server.
404 Not Found in Rest API
HTTP/1.1 404 Not Found Content-Type: text/html <html> <head><title>404 Not Found</title></head> <body> <h1>Not Found</h1> <p>The requested URL was not found on this server.</p> </body> </html>
The first line is the status line with code 404 and reason phrase 'Not Found'.
The body usually contains a message to explain the error to the user.
HTTP/1.1 404 Not Found Content-Type: application/json {"error": "Resource not found"}
HTTP/1.1 404 Not Found Content-Type: text/html <html><body><h1>404 Not Found</h1></body></html>
This Python program creates a simple web server. It responds with 'Hello, world!' when you visit /hello. For any other URL, it sends a 404 Not Found response with a message.
from http.server import BaseHTTPRequestHandler, HTTPServer class SimpleHandler(BaseHTTPRequestHandler): def do_GET(self): if self.path == '/hello': self.send_response(200) self.send_header('Content-type', 'text/plain') self.end_headers() self.wfile.write(b'Hello, world!') else: self.send_response(404) self.send_header('Content-type', 'text/plain') self.end_headers() self.wfile.write(b'404 Not Found: The requested resource was not found.') if __name__ == '__main__': server = HTTPServer(('localhost', 8000), SimpleHandler) print('Server running on http://localhost:8000') server.serve_forever()
404 is a client error status code, meaning the client asked for something that does not exist.
Always provide a helpful message or page so users know what happened.
Custom 404 pages improve user experience by guiding users back to working parts of the site.
404 Not Found means the server cannot find the requested resource.
It is used when a URL or API endpoint does not exist.
Servers should send a clear message to help users understand the error.
Practice
404 Not Found indicate in a REST API?Solution
Step 1: Understand HTTP status codes
HTTP status codes communicate the result of a client's request to the server.Step 2: Identify meaning of 404
404 means the server cannot find the requested resource or URL.Final Answer:
The requested resource does not exist on the server. -> Option AQuick Check:
404 = Resource not found [OK]
- Confusing 404 with 500 internal server error
- Thinking 404 means unauthorized access
- Assuming 404 means request succeeded
Solution
Step 1: Identify correct HTTP status line for 404
The status line must start with HTTP version and '404 Not Found' to indicate missing resource.Step 2: Check headers format
Headers like Content-Type follow the status line, separated by CRLF, then a blank line before body.Final Answer:
HTTP/1.1 404 Not Found\r\nContent-Type: application/json\r\n\r\n -> Option CQuick Check:
Status line with 404 and proper headers = HTTP/1.1 404 Not Found\r\nContent-Type: application/json\r\n\r\n [OK]
- Using 200 OK status for missing resource
- Mixing 404 with 500 or 403 status codes
- Incorrect header formatting or missing blank line
from flask import Flask, jsonify, abort
app = Flask(__name__)
@app.route('/items/')
def get_item(item_id):
items = {1: 'apple', 2: 'banana'}
if item_id not in items:
abort(404)
return jsonify({'item': items[item_id]})What will be the HTTP status code if a client requests
/items/3?Solution
Step 1: Check if item_id 3 exists in items dictionary
Items only have keys 1 and 2, so 3 is missing.Step 2: Understand abort(404) behavior
Calling abort(404) sends a 404 Not Found response to the client.Final Answer:
404 Not Found -> Option AQuick Check:
Missing item triggers abort(404) = 404 Not Found [OK]
- Assuming 200 OK even if item missing
- Confusing abort(404) with server error
- Thinking 400 Bad Request applies here
app.get('/users/:id', (req, res) => {
const users = {1: 'Alice', 2: 'Bob'};
const user = users[req.params.id];
if (!user) {
res.status(404);
}
res.send(user);
});What is the problem with this code when a user is not found?
Solution
Step 1: Analyze response when user not found
res.status(404) sets status but does not end response or send body.Step 2: Check what res.send(user) does
res.status(404) sets the status, but execution continues to res.send(undefined), which sends the string 'undefined' as body with 404 status.Final Answer:
It sends a 404 status but still sends undefined as response body. -> Option BQuick Check:
Missing res.send after status causes wrong response [OK]
- Setting status without sending response body
- Assuming status alone ends response
- Not checking for undefined response body
Solution
Step 1: Check how 404 and message are sent
app.get('/books/:id', (req, res) => { const books = {1: '1984', 2: 'Brave New World'}; const book = books[req.params.id]; if (!book) { res.status(404).json({error: 'Book not found'}); return; } res.json({book}); }); sets status 404 and sends JSON with error message, then returns to avoid sending more.Step 2: Verify other options
app.get('/books/:id', (req, res) => { const books = {1: '1984', 2: 'Brave New World'}; const book = books[req.params.id]; if (!book) { res.json({error: 'Book not found'}); } res.status(404).json({book}); }); sends JSON without status 404 first, then tries to send 404 with book JSON (wrong order). app.get('/books/:id', (req, res) => { const books = {1: '1984', 2: 'Brave New World'}; const book = books[req.params.id]; if (!book) { res.status(404); } res.json({book}); }); sets status but does not send response properly. app.get('/books/:id', (req, res) => { const books = {1: '1984', 2: 'Brave New World'}; const book = books[req.params.id]; if (!book) { res.sendStatus(404); } res.json({book}); }); sends 404 status but then sends book JSON anyway.Final Answer:
Sets status(404).json({error: 'Book not found'}); return; to send 404 with helpful JSON message and end response. -> Option DQuick Check:
Set status, send JSON, and return to end response = app.get('/books/:id', (req, res) => { const books = {1: '1984', 2: 'Brave New World'}; const book = books[req.params.id]; if (!book) { res.status(404).json({error: 'Book not found'}); return; } res.json({book}); }); [OK]
- Not returning after sending 404 response
- Sending response body after status without ending
- Mixing order of status and response methods
Ready to prove it?
Pass the test and this pattern moves unknown
