Uploading image in assets folder using fetch and Asset API

zahraazeem98
Tourist
8 1 0

Hi, I'm trying to upload an image to my theme's asset folder using Asset API. Moreover, I have my theme's id and have taken write_theme permission as well. What I'm doing is:

  const res1 = await fetch(`https://${shop}/admin/api/2021-01/themes/${themeId}/assets.json`, {
   method: 'PUT',
   credentials: 'include',
   headers: {
     'Content-Type': 'image/png',
     "X-Shopify-Access-Token": accessToken,                      //shopify store's access token
   },
   body: JSON.stringify({"asset": {
      "key": "assets/heart.png",
      "src": "https://dd98a437f772.ngrok.io/assets/icon.png"
    }})
 });
const response1 = await res1.text();
console.log(response1);

 

Also, I have my icon.png inside the assets folder of my app. If I use res1.json(), it returns:

FetchError: invalid json response body at https://tesrun.myshopify.com/admin/api/2021-01/themes/104315879578/assets.json reason: Unexpected end of JSON input

And if I use res1.text(), it doesn't return anything and doesn't upload the image in the asset folder. 

0 Likes