0

I am trying to import some images for some products using rest rest API (POST to /V1/products/{sku}/media). Works fine for the first image for each product but always fails for the second with the following message:

Failed to save new media gallery entry." which tells me nothing.

I have checked the payload many times and the few fields that could possibly require unique values (label, position and content name) are all set to unique ones.

What am I missing?

  • You are probably sending the same picture id every time, you can remove id from json request to be auto-generated by magento. Can you post a sample json request here of the first and second image? – AVRAMESCU Razvan Feb 29 '16 at 13:43
  • In a way you are correct. I found this from reading the Magento source. For the first image I can send id 0 and it works. However if I send id 0 if there already is an image it fails when trying to verify that the image was created. I'd say this is a bug in Magento because the docs give the impression that id 0 is the right value to send. – Patrik Lundgren Feb 29 '16 at 13:46
3

I spend lot of time to understand ... I had same problem. On development my java application, using official API, first i create product with one (main image), after I trying to add more images with Your error. Solution is VERY SIMPLE. When You add more than one image, avoid to add ID in json call. (I have my own Java model which reproduce original API and NEVER use "int" => use "Integer" instead, for all keys at model) BAD:

{
  "entry": {
      "id": 0,
      "mediaType": "image",
      "position": 0,
      "disabled": false,
      "types": ["image", "small_image", "thumbnail"],
      "content":{
        "base64_encoded_data" : "_9j_4AAQSkZJRgABAQEAYABgAAD__...<cutted_here>",
        "type": "image/jpeg",
        "name":"pic_name.jpg"
    }
  }
}

Right way:

{
  "entry": {
      "mediaType": "image",
      "position": 0,
      "disabled": false,
      "types": ["image", "small_image", "thumbnail"],
      "content":{
        "base64_encoded_data" : "_9j_4AAQSkZJRgABAQEAYABgAAD__...<cutted_here>",
        "type": "image/jpeg",
        "name":"pic_name.jpg"
    }
  }
}

When You read official documentation is NO describe to right way. I've founded solution looking here: line 509, see : if (isset($entry['id'])) at ./vendor/magento/module-catalog/Model/ProductRepository.php

PHP is looking if ID is set !!!

Alberto

  • Yes I realized this as after quite a few hours. I auto generated a lot of classes from the specs and they all used int for id. I have since my post changed this but I failed to update here. – Patrik Lundgren May 29 '16 at 17:51
1

Here is a sample of json which should return you the media id on success:

{
  "entry": {
      "mediaType": "image",
      "position": 0,
      "disabled": false,
      "types": ["image", "small_image", "thumbnail"],
      "content":{
        "base64_encoded_data" : "_9j_4AAQSkZJRgABAQEAYABgAAD__...<cutted_here>",
        "type": "image/jpeg",
        "name":"pic_name.jpg"
    }
  }
}
  • Yes as I wrote in a comment above, not passing 0 as id makes it work as it should. The generated docs however made me believe that using id 0 was the way to do it but that only works if there are no media entries already for the product. – Patrik Lundgren Feb 29 '16 at 14:49
  • Passing id works as well if there are existing media files, but it needs to be unique for each images – AVRAMESCU Razvan Feb 29 '16 at 14:53
  • I tried that but got the error above so not sure that is correct. Looking at the code (GalleryManagement.php) I believe I understand why but in the end not passing an id makes it work for me so I won't dig deeper into it. – Patrik Lundgren Feb 29 '16 at 14:56
  • Alright, I reproduce it. The code is getting all images, save the new image and then getting again all images and check if the new id is not in the latest collection, which in case of success save is, of course false, then it throws that exception. Looks like a bug. Glad that it works without passing id. – AVRAMESCU Razvan Feb 29 '16 at 15:05
0

Yes you need to remove the ID field from json code, and change the Image name

  • Welcome to Magento SE. The answer is not wrong. But it also does not add anything that has not been said in the answers months ago. Please avoid redundant answers. To confirm that an existing answer is correct, you can upvote it (at least as soon as you have enough reputation for it) – Fabian Schmengler Oct 1 '16 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.