2

Working on Magento version - 2.1.5.

Hello Techies

I am working on magento 2 project and learning rest api .

I am trying to upload product image via rest api .

Url of rest api = http://127.0.0.1/megento2/index.php/rest/V1/products/{sku}/media.

Method i used POST as mention in magento 2 dev docs.

Below are the parameters :

[
  {
    "id": 0,
    "media_type": "test",
    "label": "test",
    "position": 0,
    "disabled": true,
    "types": [
      "thumbnail"
    ],
    "file": "test.png",
    "content": {
      "base64_encoded_data": "Here i have passed base64_encoded data of image",
      "type": "file/png",
      "name": "test.png"
    },
    }
  }
]

Not sure why but getting 400 Error and error message is like below :

Product could not be saved

Your help will be appreciated .

2 Answers 2

3

Its because of Price field of the product.

Follow the below steps :

  • Logged in with your magento 2 admin
  • Edit the same product for which trying to upload image by rest api
  • Check price is added or not ? Price field is blank and thats why you have face this issue .
  • Add Price for that product and Save the product
  • Clear the cahe and your issue get resolved.

For more details refer this link - https://github.com/magento/magento2/issues/5728

2
  • Strange but its working !! I have added price and now image gets solved !! Thank you so much
    – Mak David
    Commented Mar 28, 2017 at 4:59
  • Yeah Face the same issue previously !! Glad to help !! Keep helping :) Commented Mar 28, 2017 at 5:01
0

I've just had a look at my json template for this and we differ on a few things. I have:

  • "media_type": "image"
  • "types": ["image',"small_image","thumbnail"]
  • "disabled": false

Also (and I think this is key) you need to post (with possible corrections from above):

{
  "entry":{
    "id": 0,
    "media_type": "test",
    "label": "test",
    "position": 0,
    "disabled": true,
    "types": [
      "thumbnail"
    ],
    "file": "test.png",
    "content": {
      "base64_encoded_data": "Here i have passed base64_encoded data of image",
      "type": "file/png",
      "name": "test.png"
    },
    }
  }
}

Note the "entry". Why this is different to other calls I don't know.

1
  • Still getting the same error
    – Mak David
    Commented Mar 27, 2017 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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