0
0
Rest APIprogramming~15 mins

404 Not Found in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
Handling 404 Not Found in a REST API
📖 Scenario: You are building a simple REST API for a bookstore. Users can request information about books by their ID. If a book is not found, the API should respond with a 404 Not Found error.
🎯 Goal: Create a REST API endpoint that returns book details by ID. If the book ID does not exist, return a 404 Not Found response with a clear message.
📋 What You'll Learn
Create a dictionary called books with exact book IDs and titles
Create a variable called requested_id with the ID to look up
Write code to check if requested_id is in books
If found, return the book title; if not, return a 404 Not Found message
Print the final response
💡 Why This Matters
🌍 Real World
APIs often need to tell users when requested data is missing. Returning a 404 Not Found status is a standard way to do this.
💼 Career
Understanding how to handle missing data and return proper error messages is essential for backend developers and API designers.
Progress0 / 4 steps
1
Create the books data
Create a dictionary called books with these exact entries: 1: 'The Great Gatsby', 2: '1984', 3: 'To Kill a Mockingbird'.
Rest API
Need a hint?

Use curly braces to create a dictionary with integer keys and string values.

2
Set the requested book ID
Create a variable called requested_id and set it to 4 to simulate a user requesting a book that does not exist.
Rest API
Need a hint?

Assign the number 4 to the variable requested_id.

3
Check if the book exists and prepare response
Write an if statement to check if requested_id is in books. If yes, create a variable response with the book title. Otherwise, set response to the string '404 Not Found: Book does not exist.'.
Rest API
Need a hint?

Use if requested_id in books: to check existence, then assign response accordingly.

4
Print the response
Write a print statement to display the response variable.
Rest API
Need a hint?

Use print(response) to show the message.