Shopify 2023-04版REST Admin API资产更新失败(404错误)
解决Shopify REST Admin Assets API PUT更新404错误问题
问题概述
使用2023-04版本Shopify REST Admin Assets API时,可通过主题ID和key正常查询到资产,但执行PUT请求更新资产时返回404 Not Found错误。
环境信息
- @shopify/shopify-api版本:2023-04
- Node版本:v18.14.2
- 操作系统:Windows
原错误代码
app.get("/api/single_a", verifyRequest, async (req, res) => { try { const sessionId = await shopify.session.getCurrentId({ rawRequest: req, rawResponse: res, }); const session = await sessionHandler.loadSession(sessionId); const client = new shopify.clients.Rest({session}); const response = await client.get({ path: 'themes/id/assets', query:{ asset:{ key: "assets/animations.js" } } }); console.log("value", response.body.asset.value ) response.body.asset.value = "<p>Heloo world </p>" console.log("value changed", response.body.asset.value ) await client.put({ path: `themes/id/assets`, data: response.body.asset }); console.log("Layout of theme liquid", response); res.status(200).json({ response }); } catch (error) { console.error("Error while fetching all Single Asset", error); res.status(500).json({ error: "An error occurred while fetching Single Asset" }); } });
错误日志
Error: Received an error response (404 Not Found) from Shopify:
"Not Found"
If you report this error, please include this id: 7bfa3dfe-cda0-46cf-a522-ee4a1410b549
问题分析
- 路径占位符未替换:代码中
path: 'themes/id/assets'和path: \themes/id/assets`里的id`是硬编码字符串,并非实际主题ID,导致请求路径无效,触发404。 - PUT请求数据格式错误:Shopify Assets API要求PUT请求的参数必须包裹在
asset对象中,原代码直接传递response.body.asset,不符合API格式要求。
修复方案
- 替换为真实主题ID:定义主题ID变量(如
const themeId = '123456789';,替换为你的实际主题ID),并在路径中使用模板字符串插入该变量。 - 修正PUT请求数据结构:将更新后的asset对象包裹在
{ asset: ... }中作为请求data。
修复后完整代码
app.get("/api/single_a", verifyRequest, async (req, res) => { try { const sessionId = await shopify.session.getCurrentId({ rawRequest: req, rawResponse: res, }); const session = await sessionHandler.loadSession(sessionId); // 替换为你的实际主题ID const themeId = 'YOUR_THEME_ID'; const client = new shopify.clients.Rest({session}); const response = await client.get({ path: `themes/${themeId}/assets`, query:{ asset:{ key: "assets/animations.js" } } }); console.log("value", response.body.asset.value ) response.body.asset.value = "<p>Hello world </p>" console.log("value changed", response.body.asset.value ) await client.put({ path: `themes/${themeId}/assets`, data: { asset: response.body.asset } }); console.log("Layout of theme liquid", response); res.status(200).json({ response }); } catch (error) { console.error("Error while updating Single Asset", error); res.status(500).json({ error: "An error occurred while updating Single Asset" }); } });
验证步骤
- 替换代码中的
YOUR_THEME_ID为店铺的真实主题ID(可在Shopify后台主题设置中查看)。 - 重启Node服务,调用
/api/single_a接口,检查控制台日志是否无404错误,同时登录Shopify后台查看对应资产内容是否已更新。
内容的提问来源于stack exchange,提问作者Syed Md Furquan
相关产品推荐
相关产品推荐

