What is 404 Status Code: Meaning and Usage in REST APIs
404 status code means "Not Found" in HTTP responses. It tells the client that the requested resource does not exist on the server.How It Works
Imagine you are looking for a book in a library, but the librarian tells you the book is not on the shelves. In web terms, when you ask a server for a webpage or data that isn't there, the server replies with a 404 status code. This code is part of the HTTP response and signals that the resource you requested cannot be found.
This helps the client (like a web browser or app) understand that the address or link they used is wrong or the resource was removed. It’s a clear way for servers to say, "Sorry, we don’t have what you asked for." This prevents confusion and helps users or programs handle missing pages gracefully.
Example
This example shows a simple HTTP server in Python that returns a 404 status code when a requested page is not found.
from http.server import BaseHTTPRequestHandler, HTTPServer class SimpleHandler(BaseHTTPRequestHandler): def do_GET(self): if self.path == '/': self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() self.wfile.write(b"<html><body><h1>Home Page</h1></body></html>") else: self.send_response(404) self.send_header('Content-type', 'text/html') self.end_headers() self.wfile.write(b"<html><body><h1>404 Not Found</h1></body></html>") if __name__ == '__main__': server = HTTPServer(('localhost', 8000), SimpleHandler) print('Server running on http://localhost:8000') server.serve_forever()
When to Use
Use the 404 status code whenever a client requests a resource that does not exist on your server. For example:
- If a user types a wrong URL or clicks a broken link.
- If a requested file or page was deleted or moved.
- When an API client asks for data that is not available.
Returning a 404 helps users and programs know the resource is missing instead of failing silently or showing incorrect data. It also improves user experience by allowing websites to show helpful "Page Not Found" messages.
Key Points
- 404 means the requested resource was not found on the server.
- It is part of the HTTP response status codes used in REST APIs and websites.
- Helps clients understand that the URL or resource is invalid or missing.
- Commonly used for broken links, deleted pages, or wrong URLs.
- Improves user experience by enabling custom "Not Found" pages.