Nested Resources in a REST API
📖 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