I am trying to create a product with a product image on drupal using API. There was nothing given regarding uploading an image in API so I created a custom module under which i used this code to upload my image to the Drupal:
$data = base64_decode($encodedData['content']);
$file = file_save_data($data, 'public://astha.jpg');
$return['id'] = $file->id();
using this my image get uploaded to the following Drupal directory successfully:
mystore2/web/sites/default/files
Now, I used this $file->id() to pass it as target id for the image in the product body as follows:
...
"field_picture" => array:1 [
"target_id" => "15"
],
...
The product gets created successfully and in response, I get the following data for the image added to the product:
...
"field_picture" => array:1 [
0 => array:8 [
"target_id" => 15
"alt" => null
"title" => null
"width" => null
"height" => null
"target_type" => "file"
"target_uuid" => "20d43ac4-1c47-4ba3-b373-16459efa5a7d"
"url" => "/file/15"
]
]
...
But when I open the drupal admin panel, the image seems to be missing or corrupted.


What should I do now?
I using Drupal version ^9.
Answers would be appreciated. Thanks.