카테고리 없음

[Strapi] GET API 사용시 미디어 정보가 보이지 않을때. (populate)

Dalmangyi 2023. 10. 28.

 

서론

Strapi는 Collection-Type을 생성하면 API를 자동으로 생성해 줍니다.

거기다가 데이터도 추가하면 조회가 되지요.

 

간단하게 유저 프로필(UserProfile) Collection-Type을 만들고,

nickname, direct_url, greetings를 텍스트 형태로 추가하고,

thumbnail, cover를 media형태로 추가합니다.

 

하지만, API호출할때 media형태의 데이터는 기본적으로 응답해서 보여주지 않습니다.

 

 

API를 호출해서 정보를 가져와봅시다.

GET {{api_url}}/user-profiles


Response
{
    "data": [
        {
            "id": 2,
            "attributes": {
                "nickname": "test",
                "direct_url": "11",
                "createdAt": "2023-10-27T18:31:53.922Z",
                "updatedAt": "2023-10-27T18:36:01.097Z",
                "greetings": "23asdfasdf"
            }
        }
    ],
    "meta": {
        "pagination": {
            "page": 1,
            "pageSize": 25,
            "pageCount": 1,
            "total": 1
        }
    }
}

data가 1개가 들어있지만

attributes쪽을 보니 media타입의 thumbnail과 cover가 없네요..

 

 

이유는?

아무래도 미디어는 종류도 많고, 가져오는 방법도 다양하기 때문에

데이터가 많다보니, 기본적으로 미디어를 빼고 알려주기 때문인데요.

이때문에 api를 호출할때 조금 귀찮더라도 파라매터(url)를 추가해줘야합니다.

 

 

 

해결방법

보고싶은 attribute이름을 아래 형태로 추가합니다.

"?populate={attribute_name}"

저희가 보고 싶은 field는 thumbnail과 cover기 때문에 아래 형태로 사용합니다.

 

URL1 :  {{api_url}}/user-profiles?populate=thumbnail,cover

선택적으로 가져오지 않고, 모든 필드값을 가져오고 싶을땐 *(all)을 사용하시면됩니다.

URL2 :  {{api_url}}/user-profiles?populate=*

 

 

URL1로 호출했을때 결과

GET {{api_url}}/user-profiles?populate=thumbnail,cover

Response
{
    "data": [
        {
            "id": 2,
            "attributes": {
                "nickname": "test",
                "direct_url": "11",
                "createdAt": "2023-10-27T18:31:53.922Z",
                "updatedAt": "2023-10-27T18:36:01.097Z",
                "greetings": "23asdfasdf",
                "thumbnail": {
                    "data": {
                        "id": 1,
                        "attributes": {
                            "name": "frislime.png",
                            "alternativeText": null,
                            "caption": null,
                            "width": 200,
                            "height": 200,
                            "formats": {
                                "thumbnail": {
                                    "ext": ".png",
                                    "url": "/uploads/thumbnail_frislime_dafa6a4525.png",
                                    "hash": "thumbnail_frislime_dafa6a4525",
                                    "mime": "image/png",
                                    "name": "thumbnail_frislime.png",
                                    "path": null,
                                    "size": 10.36,
                                    "width": 156,
                                    "height": 156
                                }
                            },
                            "hash": "frislime_dafa6a4525",
                            "ext": ".png",
                            "mime": "image/png",
                            "size": 3.95,
                            "url": "/uploads/frislime_dafa6a4525.png",
                            "previewUrl": null,
                            "provider": "local",
                            "provider_metadata": null,
                            "createdAt": "2023-10-27T18:32:36.220Z",
                            "updatedAt": "2023-10-27T18:32:36.220Z"
                        }
                    }
                },
                "cover": {
                    "data": {
                        "id": 2,
                        "attributes": {
                            "name": "제목 없는 디자인.mp4",
                            "alternativeText": null,
                            "caption": null,
                            "width": null,
                            "height": null,
                            "formats": null,
                            "hash": "_294287a3ab",
                            "ext": ".mp4",
                            "mime": "video/mp4",
                            "size": 121.61,
                            "url": "/uploads/_294287a3ab.mp4",
                            "previewUrl": null,
                            "provider": "local",
                            "provider_metadata": null,
                            "createdAt": "2023-10-27T18:35:54.330Z",
                            "updatedAt": "2023-10-27T18:35:54.330Z"
                        }
                    }
                }
            }
        }
    ],
    "meta": {
        "pagination": {
            "page": 1,
            "pageSize": 25,
            "pageCount": 1,
            "total": 1
        }
    }
}

 

thumbnail에 포함된 파일의 경로, 이미지 사이즈, 파일이름, 확장자, mime타입, hash값, url 까지 다 나오게 됩니다.

 

 

 

 

댓글