Cannot see media field in my endpoint for my content type

System Information
  • Strapi Version: v4
  • Operating System: Windows
  • Database: sqllite (quickstart option)
  • Node Version: v14.17.6
  • NPM Version: 6.14.15
  • Yarn Version: -

Hi,

I am very new Strapi and most of the research I have done is the older version. My issue here is that I have created the following content type:

notifications: [
title: "Sample Title",
image: Media upload image
]

So its very basic, however when I test out the endpoint for notifications http://localhost:1337/api/notifications I dont receive any reference to the image attribute.

{
"data": [
{
"id": 1,
"attributes": {
"title": "Testing",
"createdAt": "2021-12-03T16:04:09.535Z",
"updatedAt": "2021-12-03T16:20:36.636Z",
"publishedAt": "2021-12-03T16:04:10.329Z"
}
}
],
"meta": {
"pagination": {
"page": 1,
"pageSize": 25,
"pageCount": 1,
"total": 1
}
}
}

Like I said the older version would show this, so Iā€™m really confused. Any help would be greatly appreciated! Thanks.

3 Likes

Hi @Richard_Griffiths,

Itā€™s my second day on Strapi (Iā€™m on v4) and I have the same issue.

Hopefully the community will help.

Thanks for raising it

1 Like

@bruceg

l have managed to find the answer!

In order to get the image attributes you now have to specify the field for your media in the endpoint. Therefore my new url ended up with this:

http://localhost:1337/api/notifications?populate=image

And the response:

{
"id": 1,
"attributes": {
"title": "Testing",
"createdAt": "2021-12-03T16:04:09.535Z",
"updatedAt": "2021-12-03T16:20:36.636Z",
"publishedAt": "2021-12-03T16:04:10.329Z",
"image": {
"data": {
"id": 1,
"attributes": {
"name": "photo1.jpg",
"alternativeText": "photo1.jpg",
"caption": "photo1.jpg",
"width": 5024,
"height": 3203,
"formats": {
"thumbnail": {
"name": "thumbnail_photo1.jpg",
"hash": "thumbnail_photo1_459714b552",
"ext": ".jpg",
"mime": "image/jpeg",
"width": 245,
"height": 156,
"size": 13.06,
"path": null,
"url": "/uploads/thumbnail_photo1_459714b552.jpg"
},
"large": {},
"medium": {},
"small": {}
},
"hash": "photo1_459714b552",
"ext": ".jpg",
"mime": "image/jpeg",
"size": 1487.27,
"url": "/uploads/photo1_459714b552.jpg",
"previewUrl": null,
"provider": "local",
"provider_metadata": null,
"createdAt": "2021-12-03T16:03:55.072Z",
"updatedAt": "2021-12-03T16:03:55.072Z"
}
}
}
}
}

Hope others find this useful!

5 Likes

Hi @Richard_Griffiths have you tried to populate relations? This is failing for me. Iā€™ll investigate further.

Maybe others have tried?

Thanks

Hi @Richard_Griffiths and all - It appears that you need a full access API token.

Hi @brucegl from the documentation I read that you need the find permission to use populate.

I donā€™t know if thatā€™s the case for you.

My first day at Strapi school and had the same issue. Can someone kindly explain why on earth we need to populate this? My public Role has find and findone enable. Cos other tutorials like this donā€™t need to ā†’ Building Custom APIs With Strapi - YouTube

Why in the heck would it work like this?

Is there a way to simplay receive

"thumbnail": 184,

instead of the whole populated image?

I donā€™t need the whole image populate, only a reference. To me having at least a reference to the image id would be the strict minimum, itā€™s crazy that by default the field is simply missing.

Thanks a bunch in advance!

There is a parameter you need to pass on the query in order to receive relations and media, it is called ā€˜populateā€™

For example: https:localhost:1337/api/posts?populate=*

Note that between v3 and v4 the API path for content types has changed, and now you need to add /api/ between the content type endpoint and the root address.

On the other hand, if you are trying to get images or relations from the ā€œ/api/usersā€ or ā€œ/api/users/meā€ endpoint. You are facing a different issue that is related to a bug on the roles & permissions plugin, I just published a solution for that on reddit, see here: https://www.reddit.com/r/Strapi/comments/r7r0ie/comment/i0rub7c/?utm_source=share&utm_medium=web2x&context=3

2 Likes

that worked for me - using strapi 4, thanks!

it works.
I have a collection with a field named file which is of media type.

following is the code:

var mediaid = 12;

        fetch('http://localhost:1337/api/sample-medias/' + mediaid + "?populate=file")
        .then(function (response) {
            return response.json();
        }).then(function (data) {
            console.log(data);
        });

thanks!! you save my life