cancel
Showing results for 
Search instead for 
Did you mean: 

Error "The product can't be saved." when uploading image via API

Error "The product can't be saved." when uploading image via API

Hello,

I'm working on an integration of Magento 2 using the API. My goal at the moment is to upload an product image to the shop. Im using Magento ver. 2.4.2 and the POST /products/{SKU}/media endpoint. After not being able to do this for a while I have composed a minimal example where I try to upload one pixel of a PNG-file. The body of this Request looks like this:

{
    "entry": {
        "media_type": "image",
        "label": "Test",
        "position": 1,
        "disabled": false,
        "types": [
            "image"
        ],
        "content": {
            "base64_encoded_data": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII",
            "type": "image/png",
            "name": "pixel"
        }
    }
}

But I get the same response as before (with Status Code 400 Bad Request):

{
    "message": "The product can't be saved."
}

I can't see any syntax errors and everything else seems to work just fine. I can even use the endpoint with the GET-method to see my product images.

 

What should I do? Any ideas?

4 REPLIES 4

Re: Error "The product can't be saved." when uploading image via API

Try 

 

"types": ["image", "small_image", "thumbnail"],

instead of

 

"types": ["image"],

 

Re: Error "The product can't be saved." when uploading image via API

Hi @immelweclad627,

 

Greetings!

 

I have checked and run your API and I found that it is working properly. Check the below screenshots.


ImageImage

 

AP ResponseAP Response

 

 

You should check your product page. Make sure that the product exists when you upload an image.

 

Hope this will help you.

If still facing the issue contact us.

 

Solved? 

Click KUDOS and accept it as a solution.

Thank you!

Re: Error "The product can't be saved." when uploading image via API

Hi @namitapare2a78,

thanks for your reply. Unfortunately I can't open your screenshots. The show up as yellow trianlges. But I can tell you for sure that the product exists when I try to upload the image.

The error keeps happening regardless of the product or the types that I write into the array.

Re: Error "The product can't be saved." when uploading image via API

Hi @immelweclad627,

 

If your product exists then make sure the product you are using has all the required fields saved to the product before trying to upload the image via the rest API. 

It is important to fill every required field to get the exact response of API.

Note that this will apply to any custom-required attribute as well.

 

I hope this will somehow help you to solve your issue.

 

Solved?

Accept it as a solution and click KUDOS.

Thank you!