cancel
Showing results for 
Search instead for 
Did you mean: 

Updating product image via REST API results in two images

Updating product image via REST API results in two images

Hello,

 

I am using Magento CE 2.3.4.

 

I'm trying to update a single entry in a product's media gallery using the REST API. Basically I'm just trying to update the image with a newer image - I don't want to change any other attributes or add a new image.

 

However, when I make the PUT call, instead of updating that media entry ID with the provided information, Magento actually wipes out the metadata for the existing media entry and creates a new media entry for that product with the provided data. I then end up with two entries for the product, one of which points to the old image and the other points to the new image, whose filename is the same as the original with a "_1" appended, and both the old and new images show up on the site.

 

Any idea how to work around this?

 

Existing Data:

[
    {
        "id": 100,
        "media_type": "image",
        "label": "Product 5432 image",
        "position": 0,
        "disabled": false,
        "types": [
            "image",
            "small_image",
            "thumbnail",
            "swatch_image"
        ],
        "file": "/path/to/5432.jpg"
    }
]

API Request:

POST /rest/V1/products/5432/media/100

Payload:

{
   "entry":{
      "id":100,
      "media_type":"image",
      "label":"5432 image",
      "position":0,
      "disabled":false,
      "types":[
         "image",
         "small_image",
         "thumbnail",
         "swatch_image"
      ],
      "file":"",
      "content":{
         "base64_encoded_data":"xxx",
         "name":"5432.jpg",
         "type":"image/jpeg"
      }
   }
}

Resulting record:

[
    {
        "id": 100,
        "media_type": "image",
        "label": "5432 image",
        "position": 0,
        "disabled": false,
        "types": [],
        "file": "/path/to/5432.jpg"
    },
    {
        "id": 101,
        "media_type": "image",
        "label": "5432 image",
        "position": 0,
        "disabled": false,
        "types": [
            "image",
            "small_image",
            "thumbnail",
            "swatch_image"
        ],
        "file": "/path/to/5432_1.jpg"
    }
]
3 REPLIES 3

Re: Updating product image via REST API results in two images

As I understand it, making a POST call indicates that you're creating a new image. You need to issue a PUT call to update an existing image...

https://devdocs.magento.com/redoc/2.3/admin-rest-api.html#operation/catalogProductAttributeMediaGall...

 

Having said that, I found this question whilst looking for info as I can't actually get the PUT call to work... it results in a 503 error, even though my POST calls create new images with no problem.

Error handling on the API sucks.

Re: Updating product image via REST API results in two images

Exact same behaviour here. PUT on single mediaGallery entries creates additional ones instead of updating existing ones if the PUT payload contains entry.content. If the payload does not contain entry.content the existing mediaGallery entry is updated as expected. 

Re: Updating product image via REST API results in two images

Hi,

 

I have updated to 2.3.6 and still face the same issue. Have you managed to find a fix yet?