📖 Scenario: You are building a simple REST API for a blog. Each blog has many posts. You want to organize your API so that posts are nested inside blogs. This means the URL for posts will include the blog they belong to, like /blogs/1/posts.
🎯 Goal: Create a basic REST API with nested resources where posts belong to blogs. You will define the data, set up a route prefix for blogs, create a nested route for posts inside blogs, and finally show how to get all posts for a specific blog.
📋 What You'll Learn
Create a dictionary called blogs with two blogs, each having an id and name.
Create a list called posts with posts that include id, blog_id, and title.
Define a variable blog_id to select which blog's posts to get.
Write a function get_posts_for_blog(blog_id) that returns posts belonging to the given blog.
Print the titles of posts for the selected blog.
💡 Why This Matters
🌍 Real World
Nested resources are common in REST APIs to organize related data clearly, like posts inside blogs or comments inside posts.
💼 Career
Understanding nested resources helps you design clean and scalable APIs, a key skill for backend developers and API designers.
Progress0 / 4 steps
1
Create the initial data structures
Create a dictionary called blogs with these exact entries: 1: {'id': 1, 'name': 'Tech Blog'} and 2: {'id': 2, 'name': 'Food Blog'}. Also create a list called posts with these exact dictionaries: {'id': 1, 'blog_id': 1, 'title': 'New Python Features'}, {'id': 2, 'blog_id': 1, 'title': 'REST API Tips'}, and {'id': 3, 'blog_id': 2, 'title': 'Best Pizza Recipes'}.
Rest API
Hint
Think of blogs as a dictionary where keys are blog IDs and values are blog details. posts is a list of dictionaries, each with a blog ID to show which blog it belongs to.
2
Set the blog ID to filter posts
Create a variable called blog_id and set it to 1 to select the Tech Blog.
Rest API
Hint
This variable will help us get posts only for the selected blog.
3
Create a function to get posts for a blog
Write a function called get_posts_for_blog(blog_id) that returns a list of posts from posts where the post's blog_id matches the function's blog_id parameter.
Rest API
Hint
Use a list comprehension to filter posts where post['blog_id'] equals the function's blog_id parameter.
4
Print the titles of posts for the selected blog
Use the function get_posts_for_blog(blog_id) to get posts for the blog with blog_id. Then print each post's title on its own line.
Rest API
Hint
Call the function with blog_id and loop over the returned posts to print each title.
Practice
(1/5)
1. What is the main purpose of using nested resources in a REST API?
easy
A. To make URLs shorter and simpler
B. To show how one resource belongs inside another resource
C. To avoid using IDs in URLs
D. To separate unrelated data into different APIs
Solution
Step 1: Understand nested resources concept
Nested resources organize data by showing relationships, like a comment inside a post.
Step 2: Identify the purpose of nesting
Nesting clarifies that one resource belongs to another, helping API users find related data easily.
Final Answer:
To show how one resource belongs inside another resource -> Option B
Quick Check:
Nested resources = parent-child relationship [OK]
Hint: Nested URLs show parent-child links clearly [OK]
Common Mistakes:
Thinking nested resources make URLs shorter
Believing IDs are not needed in nested URLs
Confusing unrelated data separation with nesting
2. Which of the following is the correct nested URL format to get comments of a post with ID 5?
easy
A. /comments/5/posts
B. /comments/posts/5
C. /posts/5/comments
D. /posts/comments/5
Solution
Step 1: Identify parent and child in URL
The parent is 'posts' with ID 5, and the child is 'comments'. The child comes after the parent.
Step 2: Check URL structure
The correct format is /parent/{parent_id}/child, so /posts/5/comments is correct.
Final Answer:
/posts/5/comments -> Option C
Quick Check:
Parent ID before child resource [OK]
Hint: Parent resource and ID come before child resource in URL [OK]
Common Mistakes:
Placing child resource before parent
Swapping IDs and resource names
Using incorrect order of URL segments
3. Given the nested URL /users/10/orders/3, what resource does this URL represent?
medium
A. Order with ID 3 belonging to user with ID 10
B. User with ID 3 who placed order 10
C. All orders of user 10 and order 3 combined
D. User 10's profile and order 3 details merged
Solution
Step 1: Analyze URL segments
The URL shows 'users' with ID 10, then 'orders' with ID 3 nested inside.
Step 2: Understand resource relationship
This means order 3 belongs to user 10, so the URL points to that specific order.
Final Answer:
Order with ID 3 belonging to user with ID 10 -> Option A
Quick Check:
Nested resource = child inside parent [OK]
Hint: Read URL left to right: parent then child with IDs [OK]
Common Mistakes:
Mixing up which ID belongs to user or order
Thinking URL shows combined or merged data
Ignoring the order of resources in URL
4. You have this nested route defined: /projects/{projectId}/tasks/{taskId}. Which URL is incorrect to get task 7 of project 2?
medium
A. /projects/2/tasks/7/
B. /projects/2/tasks/7
C. /projects/2/tasks/07
D. /tasks/7/projects/2
Solution
Step 1: Check correct nested URL format
The correct format is /projects/{projectId}/tasks/{taskId}, so project ID comes before task ID.
Step 2: Identify incorrect URL
/tasks/7/projects/2 reverses the order to /tasks/7/projects/2, which breaks nesting rules.
Final Answer:
/tasks/7/projects/2 -> Option D
Quick Check:
Parent resource must come before child in URL [OK]
Using leading zeros in IDs (usually allowed but check API)
5. You want to design a REST API for a blog where each post has multiple comments, and each comment can have multiple replies. Which nested URL correctly accesses reply 4 of comment 2 on post 10?
hard
A. /posts/10/comments/2/replies/4
B. /posts/10/replies/4/comments/2
C. /comments/2/posts/10/replies/4
D. /posts/10/comments/replies/4/2
Solution
Step 1: Understand the resource hierarchy
Post is the top parent, comments belong to posts, and replies belong to comments.
Step 2: Build nested URL from parent to child
The URL should list post ID, then comment ID, then reply ID in order: /posts/10/comments/2/replies/4.
Final Answer:
/posts/10/comments/2/replies/4 -> Option A
Quick Check:
Nested URL = parent -> child -> grandchild [OK]
Hint: List resources from top parent to deepest child in URL [OK]
Common Mistakes:
Mixing order of nested resources
Skipping intermediate resource IDs
Placing child before parent in URL
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.