0

I'm using json-server to create a test API. I'd like to have make the API endpoint: http://localhost:3000/posts/1/comments

/posts and /posts/1 works fine but I'm not sure why /posts/1/comments doesn't. Am I missing something?

Here is the json

{
  "posts": [
    {
      "id": 1,
      "title": "Post 1",
      "comments": [
        {
          "id": 1,
          "body": "This is comment 1"
        }
      ]
    },
    {
      "id": 2,
      "title": "Post 2",
      "comments": []
    }
  ]
}

1 Answer 1

1

The comments are a separate object referenced by postId, as follows:

{
  "posts": [
    {
      "id": 1,
      "title": "json-server",
      "author": "typicode"
    }
  ],
  "comments": [
    {
      "id": 1,
      "body": "some comment",
      "postId": 1
    },
    {
      "id": 2,
      "body": "this is another",
      "postId": 1
    }
  ],
  "profile": {
    "name": "typicode"
  }
}

So you can do this :

localhost/posts/1

{
    id: 1,
    title: "json-server",
    author: "typicode"
}

localhost/posts/1/comments

[
    {
        id: 1,
        body: "some comment",
        postId: 1
    },
    {
        id: 2,
        body: "this is another",
        postId: 1
    }
]

http://jsonplaceholder.typicode.com/comments

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.