胡說叔叔
2021-09-30 15:02:47
概括我一直在關(guān)注關(guān)于使用 Node 和 React 設(shè)置應(yīng)用程序的 Shopify 教程。我一切正常,但我正試圖越過最后一步。我正在嘗試獲取從 Shopify webhooks 返回的數(shù)據(jù),但我看不到數(shù)據(jù)/有效負(fù)載的位置。背景我已經(jīng)嘗試在中間件之前和之后打印出響應(yīng)和請求,但似乎沒有得到任何有用的回報,只是收到如下內(nèi)容:{ method: 'POST', url: '/webhooks/products/create', header: { host: '01e9702c.ngrok.io', 'user-agent': 'Shopify-Captain-Hook', 'content-length': '1175', accept: '*/*', 'accept-encoding': 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'content-type': 'application/json', 'x-shopify-api-version': '2019-07', 'x-shopify-hmac-sha256': 'some value', 'x-shopify-product-id': '2018829729858', 'x-shopify-shop-domain': 'example.myshopify.com', 'x-shopify-topic': 'products/create', connection: 'close', 'x-forwarded-proto': 'https', 'x-forwarded-for': '123.456.789' } } router.post('/webhooks/products/create', webhook, (ctx) => { console.log('Products webhook - create: ', ctx.state.webhook); // Want to view product that was created here }); router.post('/webhooks/products/delete', webhook, (ctx, x) => { console.log('Products webhook - delete: ', ctx.state.webhook); // Want to see product that was deleted here });
2 回答

慕標(biāo)琳琳
TA貢獻1830條經(jīng)驗 獲得超9個贊
你使用的網(wǎng)絡(luò)框架是什么?是快遞嗎?添加 webhook 的代碼會有所幫助,但現(xiàn)在您的簽名已關(guān)閉。
請在此處查看我的回答Nodejs - Expressjs - 驗證 shopify webhook以了解如何使用正文解析器的驗證方法。
然后你的鉤子處理程序看起來像:
router.post('/webhooks/products/create', (req, res)=>{
console.log(JSON.stringify(req.body, null, ' '));
res.sendStatus(200);
// process the webhook
});

德瑪西亞99
TA貢獻1770條經(jīng)驗 獲得超3個贊
看起來您正在使用基本節(jié)點/反應(yīng)示例中的 Koa 框架。
假設(shè)您已經(jīng)找到它,但是任何人都在尋找,答案是ctx.state.webhook.payload
您可以從那里訪問各個元素,因此 ctx.state.webhook.payload.title
也可以使用。
添加回答
舉報
0/150
提交
取消