Magento 2 REST API – How to Attach Image to Product

apicatalogmagento2product-images

Note: Currently I have products without images, so I want to set
images for them.

URL of actual image that I'm using as base64_encoded_data: http://www.freeiconspng.com/uploads/black-iphone-6-cases-png-30.png

I'm using catalogProductAttributeMediaGalleryManagementV1(http://devdocs.magento.com/swagger/#/)

My PHP array which I'm later encoding as JSON to make post request to magento2 rest API

Array
(
    [entry] => Array
        (
            [id] => 0
            [media_type] => Image
            [label] => Iphone7
            [position] => 0
            [disabled] => 0
            [types] => Array
                (
                    [0] => image
                    [1] => small_image
                    [2] => thumbnail
                    [3] => swatch_image
                )

            [file] => /pub/media/catalog/product/i/p/iphone6.png
            [content] => Array
                (
                    [base64_encoded_data] => iVBORw0KGgoAAAANSUhEUgAAAV4AAAFeCAYAAADNK3caAAAKQ2l...
                    [type] => image/png
                    [name] => iphone6.png
                )

        )

)

My post request:

{"entry":{"id":0,"media_type":"Image","label":"Iphone7","position":0,"disabled":0,"types":["image","small_image","thumbnail","swatch_image"],"file":"\/pub\/media\/catalog\/product\/i\/p\/iphone6.png","content":{"base64_encoded_data":"iVBORw0KGgoAAAANSUhEUgAAAV4AAAFeCAYAAADNK3caAAAKQ2lDQ1BJQ0MgcHJvZmlsZQAAeNqdU3dYk\","type":"image\/png","name":"iphone6.png"}}}

I am getting this response from API:

{"message":"There is no MediaGalleryEntryConverter for given type","trace":"#0 \/var\/www\/html\/magento\/vendor\/magento\/module-catalog\/Model\/Product.php(2562): Magento\\Catalog\\Model\\Product\\Attribute\\Backend\\Media\\EntryConverterPool->getConverterByMediaType('Image')\n#1 \/var\/www\/html\/magento\/vendor\/magento\/module-catalog\/Model\/Product\/Gallery\/GalleryManagement.php(68): Magento\\Catalog\\Model\\Product->setMediaGalleryEntries(Array)\n#2 [internal function]: Magento\\Catalog\\Model\\Product\\Gallery\\GalleryManagement->create('iphone6', Object(Magento\\Catalog\\Model\\Product\\Gallery\\Entry))\n#3 \/var\/www\/html\/magento\/vendor\/magento\/module-webapi\/Controller\/Rest.php(307): call_user_func_array(Array, Array)\n#4 \/var\/www\/html\/magento\/vendor\/magento\/module-webapi\/Controller\/Rest.php(216): Magento\\Webapi\\Controller\\Rest->processApiRequest()\n#5 \/var\/www\/html\/magento\/var\/generation\/Magento\/Webapi\/Controller\/Rest\/Interceptor.php(24): Magento\\Webapi\\Controller\\Rest->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#6 \/var\/www\/html\/magento\/vendor\/magento\/framework\/App\/Http.php(135): Magento\\Webapi\\Controller\\Rest\\Interceptor->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#7 \/var\/www\/html\/magento\/vendor\/magento\/framework\/App\/Bootstrap.php(258): Magento\\Framework\\App\\Http->launch()\n#8 \/var\/www\/html\/magento\/index.php(39): Magento\\Framework\\App\\Bootstrap->run(Object(Magento\\Framework\\App\\Http))\n#9 {main}"}

Best Answer

Array
(
    [entry] => Array
        (
            [id] => 0 // No need to use this id, this would be auto generated
            [media_type] => Image // Use image instead of Image
            [label] => Iphone7
            [position] => 0
            [disabled] => 0
            [types] => Array
                (
                    [0] => image
                    [1] => small_image
                    [2] => thumbnail
                    [3] => swatch_image
                )

            [file] => /pub/media/catalog/product/i/p/iphone6.png
            [content] => Array
                (
                    [base64_encoded_data] => iVBORw0KGgoAAAANSUhEUgAAAV4AAAFeCAYAAADNK3caAAAKQ2l...
                    [type] => image/png
                    [name] => iphone6.png
                )

        )

)

Implement this array then run your code.

Related Topic