1
votes

I am beginner to Shopware and following documentation most of the time. According to the documentation for REST API https://docs.shopware.com/en/shopware-platform-dev-en/how-to/working-with-the-api-and-an-http-client

I can retrieve product data by calling the request function

$this->restService->request('GET', 'product');

Basically, it makes following request which is returning me product data.

private function createShopwareApiRequest(string $method, string $uri, ?string $body = null): RequestInterface
{
    return new Request(
        $method,
        getenv('APP_URL') . '/api/v3/' . $uri,
        [
            'Authorization' => 'Bearer ' . $this->accessToken,
            'Accept' => '*/*'
        ],
        $body
    );
}

I couldn't find the media files related to the product. Can anybody help me how can I fetch products along with the images ?

1

1 Answers

2
votes

You need to add association parameter to your request. In the case of GET product request it should look like this /api/v3/product?associations[media][]

When you did it in product response you will get objects like this:

"media": {
            "data": [
                {
                    "type": "product_media",
                    "id": "41e9b70e1df84b999bbf08ce7bf3fb77"
                }
            ],
            "links": {
                "related": "http://localhost:8000/api/v3/product/a1d20f10d019491fbf889ad5651aab23/media"
            }
        },

by product media id, you can find the full object in included object of your response and real mediaId inside. Then search media object in included by mediaId.

{
        "id": "41e9b70e1df84b999bbf08ce7bf3fb77",
        "type": "product_media",
        "attributes": {
            "versionId": "0fa91ce3e96a4bc2be4bd9ce752c3425",
            "productId": "a1d20f10d019491fbf889ad5651aab23",
            "productVersionId": "0fa91ce3e96a4bc2be4bd9ce752c3425",
            "mediaId": "1bf12b4bae5e4d288ce049aacfd2cc24",
            "position": 1,
            "customFields": null,
            "createdAt": "2020-05-22T13:10:37.255+00:00",
            "updatedAt": null,
            "apiAlias": null
        }
},
{
        "id": "1bf12b4bae5e4d288ce049aacfd2cc24",
        "type": "media",
        "attributes": {
            "userId": null,
            "mediaFolderId": "fd22d1ef41994f6ea05f9b4cb01d85d3",
            "mimeType": "image/jpeg",
            "fileExtension": "jpg",
            "uploadedAt": "2020-05-22T13:10:37.227+00:00",
            "fileName": "10062415_1",
            "fileSize": 74503,
            "metaData": {
                "type": 2,
                "width": 1500,
                "height": 1000
            },
            "mediaType": {
                "name": "IMAGE",
                "flags": [],
                "extensions": []
            }
}