2

I'm trying to set a product image using the web api by POSTing to this url:

http://my_magento_server/rest/V1/products/my-url-key/media

With the following content:

{"entry":
  {
    "media_type": "image",
    "label": "Image",
    "position": 1,
    "disabled": false,
    "types": [
      "image",
      "small_image",
      "thumbnail"
    ],
    "file": "/m/b/mb01-blue-0.jpg",
    "content": {
      "base64EncodedData": "iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAWtJREFUeNpi/P//P8NgBkwMgxyMOnDUgTDAyMhIDNYF4vNA/B+IDwCxHLoakgEoFxODiQRXQUYi4e3k2gfDjMRajsP3zED8F8pmA+JvUDEYeArEMugOpFcanA/Ef6A0CPwC4uNoag5SnAjJjGI2tKhkg4rLAfFGIH4IxEuBWIjSKKYkDfZCHddLiwChVhokK8YGohwEZYy3aBmEKmDEhOCgreomo+VmZHxsMEQxIc2MAx3FO/DI3RxMmQTZkI9ALDCaSUYdOOrAIeRAPzQ+PxCHUM2FFDb5paGNBPRa5C20bUhxc4sSB4JaLnvxVHWHsbVu6OnACjyOg+HqgXKgGRD/JMKBoD6LDb0dyAPE94hwHAw/hGYcujlwEQmOg+EV9HJgLBmOg+FMWjsQVKR8psCBoDSrQqoDSSmoG6Hpj1wA6ju30LI9+BBX4UsC+Ai0T4BWVd1EIL5PgeO+APECmoXgaGtm1IE0AgABBgAJAICuV8dAUAAAAABJRU5ErkJggg==",
      "type": "image/jpeg",
      "name": "new image"
    }
  }
}

But I get 400 response:

The image MIME type is not valid or not supported

Any idea how I can upload a file as product image in Magento 2?

3
  • image/jpeg should be image/jpg Commented Apr 4, 2016 at 7:07
  • @Arunendra still getting the same error Commented Apr 4, 2016 at 7:08
  • Hello Michael You found solution ? If you have solution please share me. I am image upload via rest api i geting ''message": "Provided image name contains forbidden characters." error. Commented Apr 17, 2019 at 12:48

8 Answers 8

4

After base64 decoding the image you provided, I noticed it is the PNG image, not JPEG.

Try changing the "type" attribute in your JSON to "image/png".

Additionally, you should add file extension to your "name" attribute.

The example below worked fine for me.

{
        "entry": {
            "media_type": "image",
            "label": "Image",
            "position": 1,
            "disabled": false,
            "types": [
                "image",
                "small_image",
                "thumbnail"
            ],
            "file": "/m/b/mb01-blue-0.png",
            "content": {
                "base64EncodedData": "iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAWtJREFUeNpi/P//P8NgBkwMgxyMOnDUgTDAyMhIDNYF4vNA/B+IDwCxHLoakgEoFxODiQRXQUYi4e3k2gfDjMRajsP3zED8F8pmA+JvUDEYeArEMugOpFcanA/Ef6A0CPwC4uNoag5SnAjJjGI2tKhkg4rLAfFGIH4IxEuBWIjSKKYkDfZCHddLiwChVhokK8YGohwEZYy3aBmEKmDEhOCgreomo+VmZHxsMEQxIc2MAx3FO/DI3RxMmQTZkI9ALDCaSUYdOOrAIeRAPzQ+PxCHUM2FFDb5paGNBPRa5C20bUhxc4sSB4JaLnvxVHWHsbVu6OnACjyOg+HqgXKgGRD/JMKBoD6LDb0dyAPE94hwHAw/hGYcujlwEQmOg+EV9HJgLBmOg+FMWjsQVKR8psCBoDSrQqoDSSmoG6Hpj1wA6ju30LI9+BBX4UsC+Ai0T4BWVd1EIL5PgeO+APECmoXgaGtm1IE0AgABBgAJAICuV8dAUAAAAABJRU5ErkJggg==",
                "type": "image/png",
                "name": "new_image.png"
            }
        }
    }
1
  • file , "/m/b/mb01-blue-0.png" does not seems relevant since magento creates this path automatically. I wonder if it can be used to replace an existing previously posted image. Commented Aug 11, 2017 at 10:00
1

This a Megento Bug still unresolved. I fixed this one by wrapping this condition in MAGE_ROOT_FOLDER/vendor/magento/framework/Api/ImageContentValidator.php

if ($sourceMimeType != $imageContent->getType() || !$this->isMimeTypeValid($sourceMimeType)) {
            throw new InputException(new Phrase('The image MIME type is not valid or not supported.'));
        }

inside another condition like this

if ($sourceMimeType != 'image/png') {
        if ($sourceMimeType != $imageContent->getType() || !$this->isMimeTypeValid($sourceMimeType)) {
            throw new InputException(new Phrase('The image MIME type is not valid or not supported.'));
        }
    }

I know that modifying any of the files in vendor folder is a bad idea but while this issue remains unfixed there is something you should do to get the job done!

1

I noticed that its image/jpg which causes this problem. I simply replace the type as image/jpeg when the extension is jpg and it works like a charm.

//Getting path info $contentType = pathinfo($saved_path, PATHINFO_EXTENSION);

if($contentType == 'jpg') $contentType="jpeg";

$content_Type = 'image/'.$contentType;

0

I think the name must contain the filename extension. Try it with "name": "new image.jpg" or "name": "new image.png"

I also have a problem, I can't upload my images as thumbnails. I must set this manually.

{
  "entry": {
    "mediaType": "image",
    "disabled": "false",
    "types": [
      "image",
      "small_image",
      "thumbnail"
    ],
    "content": {
      "base64EncodedData": "...", 
      "type": "image/jpeg",
      "name": "image.jpg"
    }
  }
}
0

You did one thing wrong.You don't need to use "file": "/m/b/mb01-blue-0.jpg", in your request packet. It is generated by Magento itself.Final packet should be look like this..

{
    "entry": {
        "media_type": "image",
        "label": "Image",
        "position": 1,
        "disabled": false,
        "types": [
            "image",
            "small_image",
            "thumbnail"
        ],

        "content": {
            "base64EncodedData": "iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAWtJREFUeNpi/P//P8NgBkwMgxyMOnDUgTDAyMhIDNYF4vNA/B+IDwCxHLoakgEoFxODiQRXQUYi4e3k2gfDjMRajsP3zED8F8pmA+JvUDEYeArEMugOpFcanA/Ef6A0CPwC4uNoag5SnAjJjGI2tKhkg4rLAfFGIH4IxEuBWIjSKKYkDfZCHddLiwChVhokK8YGohwEZYy3aBmEKmDEhOCgreomo+VmZHxsMEQxIc2MAx3FO/DI3RxMmQTZkI9ALDCaSUYdOOrAIeRAPzQ+PxCHUM2FFDb5paGNBPRa5C20bUhxc4sSB4JaLnvxVHWHsbVu6OnACjyOg+HqgXKgGRD/JMKBoD6LDb0dyAPE94hwHAw/hGYcujlwEQmOg+EV9HJgLBmOg+FMWjsQVKR8psCBoDSrQqoDSSmoG6Hpj1wA6ju30LI9+BBX4UsC+Ai0T4BWVd1EIL5PgeO+APECmoXgaGtm1IE0AgABBgAJAICuV8dAUAAAAABJRU5ErkJggg==",
            "type": "image/png",
            "name": "new_image.png"
        }
    }
}

Try it. It will work.

0

I got this error on a perfectly fine image: I had "type": "image/jpeg" and the content was indeed JPEG. The problem was that I had a dot in the image name: IMG1234.56. Magento interprets it as a file extension and thus return this error. The fix was to change that dot to another character, like _.

0

The issue is that magento checks the content->type value, and only accepts jpeg and not jpg. so even if your image is a .jpg, you need to build this data value as 'image/jpeg'..

0

I encountered the same scenario and resolved it by sending the image type following the MINE standards, where I sent a .jpg image converted to base64 with MINE TYPE ("type": "image/jpeg", "name": "image.jpeg"

The result was the image sent to the product.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.