Score:0

Image is not showing on the product edit page after creating the product using API

th flag

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.

enter image description here

enter image description here

What should I do now? I using Drupal version ^9.

Answers would be appreciated. Thanks.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.