[v4] How to fetch the image from of an objects component via rest api?

System Information
  • Strapi Version:4.0.6
  • Database:postgres

Hi,

I am trying to fetch the image from a component array of a content-type. I have a content-type called office-info, within it I have a component array of Floors, the floors have an image field called FloorPlanImage. When I try to query using http://localhost:1337/api/office-infos?populate=* I get data that looks like this.

        {
            "id": 1,
            "attributes": {
                "name": "Toronto",
                "Floors": [
                    {
                        "id": 1,
                        "FloorName": "4th Floor"
                    }
                ]
            }
        }

It is missing the image field FloorPlanImage which is defined.

I can query the information I need with the graphql API with this query

{
  officeInfos {
    data {
      attributes {
	Floors {
          FloorPlanImage {
	    data {
              attributes {
                formats
              }
            }
          }
        }
      }
    }
  }
}

What would be the rest equivalent call to get the image tied to each floor?

Thanks!

Hey, populate="*" is equivalent to " Populate 1 level for all relations". But here you want to populate on two levels.
I think what you can try is either:

  • populate 2 levels for all relations: /api/office-infos?populate[0]=*&populate[1]=* but I’m not sure if this is supported tbh.

  • The better approach would be to only populate what you need: /api/articles?populate[0]=floors&populate[1]=floors.floorPlanImage

You can use the qs library to simplify things.

See docs: Population for REST API - Strapi Developer Docs

Perfect! Thank you, my googling failed to find that documentation.