GET /api/v1/posts/84/comments • HTTP 200 OK

Nested Sub-Resource API (Post Comments) — Request & Response Example

Demonstrates hierarchical REST relationships such as querying comments that belong specifically to blog post #84.

1. HTTP Client Request (GET)

GET /api/v1/posts/84/comments HTTP/1.1
{
  "Accept": "application/json"
}

2. Server JSON Response (HTTP 200 OK)

[
  {
    "comment_id": "c-301",
    "post_id": 84,
    "author": "Elena Gomez",
    "comment": "This API explanation helped our entire junior dev team grasp HTTP verbs!",
    "created_at": "2026-10-06T10:15:00Z"
  },
  {
    "comment_id": "c-302",
    "post_id": 84,
    "author": "David Vance",
    "comment": "Very clean request and response documentation.",
    "created_at": "2026-10-06T11:20:00Z"
  }
]

Copy-Paste Code Snippets (cURL, JavaScript Fetch, Python, Node.js)

curl -X GET "https://cometflow.space/api/v1/posts/84/comments"
fetch('https://cometflow.space/api/v1/posts/84/comments')
  .then(res => res.json())
  .then(comments => console.log(comments));
import requests

res = requests.get('https://cometflow.space/api/v1/posts/84/comments')
print(res.json())

How This API Call Works Step-by-Step

  1. 1. URL Hierarchy: /posts/:postId/comments indicates a one-to-many relationship.
  2. 2. Database Join: Server filters comments where post_id = 84.
  3. 3. Array Response: Returns all child comments associated with the parent post.

Backend API Functions: The API function getCommentsByPostId(req, res) extracts req.params.postId and queries the comments table by foreign key.

Frequently Asked Questions

How deep should nested API routes go?

Best practice is to avoid nesting deeper than 2 levels (e.g. /posts/:id/comments is good; /users/:id/posts/:id/comments/:id/likes is too deep and should be flattened to /comments/:id/likes).