0
0
Rest APIprogramming~5 mins

Nested resources in Rest API

Choose your learning style9 modes available
Introduction

Nested resources help organize related data in a clear way. They show how one thing belongs inside another.

You have a blog with posts and each post has comments.
You manage a store with categories and products inside each category.
You want to show orders and the items inside each order.
You have users and each user has multiple addresses.
You want to keep track of projects and tasks within each project.
Syntax
Rest API
/parent_resource/{parent_id}/child_resource/{child_id}
The URL shows the child resource inside the parent resource.
IDs help find the exact parent and child items.
Examples
Get comment 456 that belongs to post 123.
Rest API
/posts/123/comments/456
Access address 7 of user 42.
Rest API
/users/42/addresses/7
List all products inside category 5.
Rest API
/categories/5/products
Sample Program

This request asks for chapter 3 inside book 10.

Rest API
GET /books/10/chapters/3 HTTP/1.1
Host: example.com

OutputSuccess
Important Notes

Nested URLs make it easy to understand relationships between data.

Keep URLs simple and consistent for better API design.

Summary

Nested resources show how one item belongs inside another.

Use nested URLs to organize related data clearly.

Include parent and child IDs to find exact resources.