Magento2 REST API – How to POST Picture/Media

apimagento2media-imagesrest

I am trying to POST to

192.168.2.153/rest/V1/products/{MYSKU}/media

I used several online picture to BASE64 converter to convert to BASE64.

The first line looks like

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAA

And here is my body (without the BASE64 code to save space here):

{
"entry": {
"media_type": "image",
"label": "new_picture",
"types": [
  "image"
],
"file": "pic1.jpg",
"content": {
  "base64_encoded_data": "",
  "type": "image",
  "name": "pic1.jpg"
}
  }
}

I am only getting

The image content must be valid base64 encoded data.

Whatever I try no luck.

Best Answer

The base64_encoded_data should start with /9j/4AAQSkZJRgABAQEAYABgAA, no need to add data:image/jpeg;base64, before the type also should be image/jpeg

the final json should be

{
  "entry": {
    "media_type": "image",
    "label": "new_picture",
    "types": [
      "image"
    ],
    "file": "pic1.jpg",
    "content": {
      "base64_encoded_data": "/9j/4AAQSkZJRgABAQEAYABgAA.........",
      "type": "image/jpeg",
      "name": "pic1.jpg"
    }
  }
}