📖 Scenario: You are building a simple REST API server that allows users to read information about books in a library.Users will send GET requests to your API to get details about books.
🎯 Goal: Create a REST API endpoint that handles GET requests to read book information from a predefined list of books.
📋 What You'll Learn
Create a list of books with exact titles and authors
Add a variable to hold the API endpoint path
Write code to handle GET requests and return the correct book data
Print the response data to show the result of the GET request
💡 Why This Matters
🌍 Real World
APIs often provide data to users or other programs using GET requests to read resources like books, products, or users.
💼 Career
Understanding how to handle GET requests and return data is a fundamental skill for backend developers and API designers.
Progress0 / 4 steps
1
DATA SETUP: Create a list of books
Create a list called books with these exact dictionaries: {'id': 1, 'title': '1984', 'author': 'George Orwell'}, {'id': 2, 'title': 'To Kill a Mockingbird', 'author': 'Harper Lee'}, and {'id': 3, 'title': 'The Great Gatsby', 'author': 'F. Scott Fitzgerald'}.
Rest API
Hint
Remember to create a list named books with three dictionaries exactly as shown.
2
CONFIGURATION: Define the API endpoint path
Create a variable called endpoint and set it to the string "/books".
Rest API
Hint
Set endpoint exactly to the string "/books".
3
CORE LOGIC: Handle GET request to return books
Write a function called get_books that takes no parameters and returns the books list.
Rest API
Hint
Define a function named get_books that returns the books list.
4
OUTPUT: Print the result of the GET request
Call the function get_books() and print its result.
Rest API
Hint
Use print(get_books()) to show the list of books.
Practice
(1/5)
1. What is the main purpose of a GET request in REST API?
easy
A. To read or fetch data from the server
B. To create new data on the server
C. To update existing data on the server
D. To delete data from the server
Solution
Step 1: Understand the HTTP methods
GET is one of the HTTP methods used in REST APIs. It is specifically designed to retrieve data without changing it.
Step 2: Identify the purpose of GET
GET requests fetch or read data from the server without modifying it, unlike POST, PUT, or DELETE.
Final Answer:
To read or fetch data from the server -> Option A
Quick Check:
GET = Read data [OK]
Hint: GET always fetches data without changing it [OK]
Common Mistakes:
Confusing GET with POST which creates data
Thinking GET deletes data
Assuming GET updates data
2. Which of the following is the correct syntax for a GET request URL to fetch a user with ID 5?
easy
A. POST /users/5
B. POST /users?id=5
C. GET /users/5
D. DELETE /users/5
Solution
Step 1: Identify the HTTP method for reading
GET is the correct method to read or fetch data, so the method must be GET.
Step 2: Check the URL path for resource
/users/5 is the typical RESTful path to get a user with ID 5. Other options use wrong HTTP methods.
Final Answer:
GET /users/5 -> Option C
Quick Check:
GET + resource path = correct syntax [OK]
Hint: GET method + resource path fetches data [OK]
Common Mistakes:
Using POST or DELETE instead of GET
Using query parameters incorrectly
Mixing HTTP methods and URLs
3. Given the following GET request URL: /products?category=books&limit=3, what does the server most likely return?
medium
A. All products without filtering
B. Products with category 'limit' and quantity 3
C. An error because of invalid query parameters
D. First 3 products in the books category
Solution
Step 1: Analyze the query parameters
The URL has two query parameters: category=books and limit=3, which filter products by category and limit the number returned.
Step 2: Understand typical API behavior
APIs commonly use 'category' to filter items and 'limit' to restrict the number of results returned.
Final Answer:
First 3 products in the books category -> Option D