above code adds new route to your existing post api .
change post.js in controllers folder
'use strict';
const { createCoreController } = require('@strapi/strapi').factories;
module.exports = createCoreController('api::post.post', ({strapi}) => ({
comments: async (ctx) => {
// following variable will get url `id` param value.
const id = ctx.params['id']
}
}));
One Important thing : If you are interacting with this api then please generate api token or give permission to existing api token with token type (access) as Full access otherwise you will get Forbidden Error .
Thank you nitin_tejuja. I feel I am moving forwards… I just replaced POST with GET, because I am trying to get comments from my post. Still, I have a 404 error :
@Mimitip yes and since strapi is giving by default /api to api requests means
default http://localhost:1337/api and since you defined route url is /api/posts/:id/comments so now to work ,actual url should be http://localhost:1337/api/api/posts/3/comments
So if you delete /api from route url and put route url as /posts/:id/comments then it will work as you are expecting .
Ok. I replaced path: ‘/api/posts/:id/comments’ with path: ‘/posts/:id/comments’ in post.js existing in routes folder. I tested GET http://localhost:1337/api/posts/:id/comments with Postman. I have the same error… but I feel I am not far to have the right result.
well i think there is one mistake replace get router() with get routes() in your post.js in routes folder ,as default router will not able to find method get router() .
same scenario i was able to accomplish in my machine . are you using api token for authentication for api requests ? or have you changed any prefix url configuration in config/server.js ?