Magento 2 - How to upload image to REST API (via POST request)
If you want to upload a product image using Rest API you need to pass the below parameters in your request:
post to https://{YOUDOMAIN}/rest/V1/products/{SKU}/media with body, JSON:
{
"entry": {
"media_type": "image",
"label": null,
"position": 5,
"disabled": false,
"types": [
"image",
"small_image",
"thumbnail"
],
"file": "test.jpg",
"content": {
"base64_encoded_data": "{BASE64_IMAGE}",
"type": "image/jpeg",
"name": "test.jpg"
}
}
}
And if you want, before sending an image, you can encode it to base64 like this:
<?php
$path = 'path/to/image.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
Note: you need to have a header "Authorization" with "Bearer {Magento 2 TOKEN}". How to get it? Just send POST request with your email and password to https://{YOURDOMAIN}/rest/V1/integration/admin/token?username={ADMIN_NAME}&password={ADMIN_PASSWORD} or generate a token via Magento 2 admin panel.