reactjsnext.jsstrapiheadless-cms

why post categories are not included in API of Strapi?


I've made these content types : post and category and category has a many-to-many relation to post content type. buy I can't get each post category via API!

Text
API URL I use to get all posts :

ALL_POSTS = "http://192.168.242.80:4001/api/posts/"  

response type I get :

{
   "data":{
      "id":5,
      "attributes":{
         "title":"",
         "description"",
         "tag":"",
         "slug":"",
         "createdAt":"2023-03-15T05:39:04.763Z",
         "updatedAt":"2023-03-25T08:19:32.682Z",
         "publishedAt":"2023-03-15T05:39:05.953Z"
      }
   },
   "meta":{
      
   }  

as you see , category of the posts are not included! just tag field is included but tag is an enumeration field that is not suitable for my purpose
Text


Solution

  • to do that, we need to populate relations. check the link bellow and read the Strapi doc about populations: https://docs.strapi.io/dev-docs/api/rest/populate-select#populate-1-level-for-all-relations

    using these kind of queries will help:

    GET /api/articles?populate=*