📖 Scenario: You are building a simple REST API for a library system. The system has books and each book has chapters. You want to create hierarchical resource paths to access chapters of specific books.
🎯 Goal: Build a REST API with hierarchical resource paths to get chapters of a specific book using URL paths like /books/{book_id}/chapters/{chapter_id}.
📋 What You'll Learn
Create a dictionary called library with books and their chapters
Create a variable called book_id to select a specific book
Use a hierarchical path string to access a chapter of the selected book
Print the chapter title using the hierarchical resource path
💡 Why This Matters
🌍 Real World
Hierarchical resource paths are used in REST APIs to organize and access nested resources like books and chapters.
💼 Career
Understanding hierarchical paths helps in designing clear and scalable APIs for web services.
Progress0 / 4 steps
1
Create the library data structure
Create a dictionary called library with these exact entries: "book1" with chapters {"chapter1": "Introduction", "chapter2": "Basics"} and "book2" with chapters {"chapter1": "Getting Started", "chapter2": "Advanced Topics"}.
Rest API
Hint
Think of library as a dictionary where each book ID maps to another dictionary of chapters.
2
Select a book using a variable
Create a variable called book_id and set it to the string "book1" to select the first book.
Rest API
Hint
Use a simple string variable to hold the book ID.
3
Access a chapter using hierarchical resource path
Create a variable called chapter_title that accesses the chapter with key "chapter2" inside the book selected by book_id from the library dictionary.
Rest API
Hint
Use two square brackets to access nested dictionaries: first for book, second for chapter.
4
Print the chapter title
Write a print statement to display the value of chapter_title.
Rest API
Hint
Use print() to show the chapter title on the screen.
Practice
(1/5)
1. What does a hierarchical resource path in a REST API represent?
easy
A. A nested structure showing relationships between resources
B. A random list of unrelated resources
C. A single resource without any relation
D. A method to encrypt API data
Solution
Step 1: Understand resource organization
Hierarchical paths show how resources are connected, like folders inside folders.
Step 2: Identify the purpose of hierarchical paths
They help represent parent-child relationships clearly in the API URL.
Final Answer:
A nested structure showing relationships between resources -> Option A
Hint: Think of folders inside folders to understand hierarchy [OK]
Common Mistakes:
Confusing hierarchy with unrelated resource lists
Ignoring the relationship between resources
Thinking it's about data encryption
2. Which of the following is the correct syntax for a hierarchical resource path to access comments of a post with ID 10?
easy
A. /posts/10/comments
B. /comments/posts/10
C. /posts/comments/10
D. /10/posts/comments
Solution
Step 1: Identify the parent resource and its ID
The parent resource is 'posts' with ID 10, so it comes first in the path.
Step 2: Append the child resource
Comments belong to the post, so '/comments' follows '/posts/10'.
Final Answer:
/posts/10/comments -> Option A
Quick Check:
Parent resource + ID + child resource = /posts/10/comments [OK]
Hint: Parent resource and ID come before child resource [OK]
Common Mistakes:
Placing child resource before parent
Swapping resource names and IDs
Using incorrect order of segments
3. Given the REST API path /users/5/orders/12/items, what resource does this path represent?
medium
A. All orders of all users
B. Order 12 of user 5
C. User 5's profile information
D. Items in order 12 of user 5
Solution
Step 1: Break down the path segments
/users/5 means user with ID 5, /orders/12 means order with ID 12 for that user, and /items means the items in that order.
Step 2: Understand the full resource
The path points to the items inside order 12 which belongs to user 5.
Final Answer:
Items in order 12 of user 5 -> Option D
Quick Check:
Path shows nested resources = items in order 12 of user 5 [OK]
Hint: Read path left to right as parent to child resources [OK]
Common Mistakes:
Ignoring the nested order and item relationship
Confusing order with user profile
Assuming it lists all users or orders
4. Identify the error in this hierarchical REST API path: /projects//tasks/7
medium
A. Missing task ID before 7
B. Double slash between 'projects' and 'tasks' is invalid
C. Resource names should be capitalized
D. The path should end with a slash
Solution
Step 1: Check the path syntax
The path has two slashes '//' between 'projects' and 'tasks', which is not valid in REST API paths.
Step 2: Understand correct path format
Only one slash should separate resource names and IDs to maintain proper hierarchy.
Final Answer:
Double slash between 'projects' and 'tasks' is invalid -> Option B
Quick Check:
Only single slashes separate path segments [OK]
Hint: Check for extra or missing slashes in paths [OK]
Common Mistakes:
Ignoring double slashes as harmless
Thinking resource names must be capitalized
Believing trailing slash is mandatory
5. You want to design a REST API path to access all reviews for a specific product variant. The product ID is 20 and the variant ID is 5. Which path correctly represents this hierarchical resource?
hard
A. /products/reviews/20/variants/5
B. /reviews/products/20/variants/5
C. /products/20/variants/5/reviews
D. /variants/5/products/20/reviews
Solution
Step 1: Identify the resource hierarchy
Reviews belong to a variant, which belongs to a product. So the path should start with product ID, then variant ID, then reviews.
Step 2: Construct the path in correct order
The correct order is /products/20/variants/5/reviews to show this nested relationship clearly.
Final Answer:
/products/20/variants/5/reviews -> Option C
Quick Check:
Parent to child order: product -> variant -> reviews [OK]
Hint: Order path segments from general to specific resource [OK]
Common Mistakes:
Placing reviews before products or variants
Mixing order of product and variant IDs
Using unrelated resource order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.