Updating theme asset using shopify rest api giving 406 Not Acceptable error

While trying to update an asset to the main theme using shopify rest api it is giving 406 Not Acceptable error. But same thing while trying through axios working properly.

Error Id : e48efcea-0cad-4138-87af-3aa0dcee0705

Through axios (Working)

let data = JSON.stringify({
      asset: {
        key: "sections/calander.liquid",
        value: "

We are busy updating the store for you and will be back within the hour.

",
      },
    });

    let config = {
      method: "put",
      url: "https://storename.myshopify.com/admin/api/2023-01/themes/THEME_ID/assets.json",
      headers: {
        "X-Shopify-Access-Token": "access_token",
        "Content-Type": "application/json",
      },
      data: data,
    };

    axios
      .request(config)
      .then((response) => {
        console.log(JSON.stringify(response.data));
      })
      .catch((error) => {
        console.log(error);
      });

Throug shopify rest (Not working giving 406 Not Acceptable error)

let shopify = shopify.api.clients.Rest({ session });

let data = JSON.stringify({
      asset: {
        key: "sections/calander.liquid",
        value: "

We are busy updating the store for you and will be back within the hour.

",
      },
    });
    
let assetPath = `themes/${mainThemeId}/assets`;

    try {
      let result = await shopify.put({
        path: assetPath,
        query: data,
      });
      console.log(result);
    } catch (error) {
      console.log(error);
    }

Can anyone please help with the issue. Thanks

Hey @Arjun-C-S - thanks for getting in touch. Looking at the info you shared here, it seems like there might be a difference in terms of how Axios accepts input and how our system handles API calls directly. In our documentation, the asset update method for REST through Node.JS should be something like this:

const asset = new shopify.rest.Asset({session: session});
asset.theme_id = 828155753;
asset.key = “templates/index.liquid”;
asset.value = “

We are busy updating the store for you and will be back within the hour.

”;
await asset.save({
update: true,
});

Can you give that method a try and let us know if you still encounter issues? If there’s a use case for using the method. you shared, please let us know too. We can definitely take a further look if need be. We are usually unable to help with code-level issues, but we can troubleshoot a bit further to see if we can help. That said, we would recommend using the method described by our dev docs as it can reduce issues. Hope this helps!

Al |Shopify Developer Support

Hello,

I’m trying to create a “section file” with the Asset resource, but keep getting a an error.

I’m testing this code but when I call asset.save({ update: true }), the error happens.

app.put('/api/sections/install', async (_req, res) => {
  try {
    const session = res.locals.shopify.session
    const asset = new shopify.api.rest.Asset({ session: session })
    asset.theme_id = 97567768712
    asset.key = 'sections/test-section.liquid'
    asset.value = '# This is a test section created from api'
    await asset.save({
      update: true,
    })
    res.status(200).send({ value: asset.value, id: asset.theme_id })
  } catch (error) {
    res.status(400).send({ message: error.message })
  }
})

The error response is:

{
  "message": "Received an error response (404 Not Found) from Shopify:\n\"Not Found\"\nIf you report this error, please include this id: f45ca7bc-9be5-4f07-9e0c-b4227da38ed8"
}

Please help me,