无法从 Shopify Webhook 找到数据/有效负载



摘要

我一直在关注 Shopify 教程关于使用 Node 和 React 设置应用程序。我一切正常,但我正在努力通过最后一步。我正在尝试获取从 Shopify 网络钩子返回的数据,但我看不到数据/有效负载的位置。

背景

我尝试在中间件之前和之后打印出响应和请求,但似乎没有得到任何有用的回报,只是收到了如下内容:

{ 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
});

预期输出

我希望收到一个 json 对象,如 Shopify Webhook 事件文档中所述。

{
"id": 788032119674292922,
"title": "Example T-Shirt",
"body_html": null,
"vendor": "Acme",
"product_type": "Shirts",
"created_at": null,
"handle": "example-t-shirt",
"updated_at": null,
"published_at": "2019-08-08T16:16:49-04:00",
"template_suffix": null,
"tags": "mens t-shirt example",
"published_scope": "web",
"variants": [
{
"id": 642667041472713922,
"product_id": 788032119674292922,
"title": "",
"price": "19.99",
"sku": "example-shirt-s",
"position": 0,
"inventory_policy": "deny",
"compare_at_price": "24.99",
"fulfillment_service": "manual",
"inventory_management": "shopify",
"option1": "Small",
"option2": null,
"option3": null,
"created_at": null,
"updated_at": null,
"taxable": true,
"barcode": null,
"grams": 200,
"image_id": null,
"weight": 200.0,
"weight_unit": "g",
"inventory_item_id": null,
"inventory_quantity": 75,
"old_inventory_quantity": 75,
"requires_shipping": true
},
{
"id": 757650484644203962,
"product_id": 788032119674292922,
"title": "",
"price": "19.99",
"sku": "example-shirt-m",
"position": 0,
"inventory_policy": "deny",
"compare_at_price": "24.99",
"fulfillment_service": "manual",
"inventory_management": "shopify",
"option1": "Medium",
"option2": null,
"option3": null,
"created_at": null,
"updated_at": null,
"taxable": true,
"barcode": null,
"grams": 200,
"image_id": null,
"weight": 200.0,
"weight_unit": "g",
"inventory_item_id": null,
"inventory_quantity": 50,
"old_inventory_quantity": 50,
"requires_shipping": true
}
],
"options": [
{
"id": 527050010214937811,
"product_id": 788032119674292922,
"name": "Title",
"position": 1,
"values": [
"Small",
"Medium"
]
}
],
"images": [
{
"id": 539438707724640965,
"product_id": 788032119674292922,
"position": 0,
"created_at": null,
"updated_at": null,
"alt": null,
"width": 323,
"height": 434,
"src": "//cdn.shopify.com/s/assets/shopify_shirt-39bb555874ecaeed0a1170417d58bbcf792f7ceb56acfe758384f788710ba635.png",
"variant_ids": [
]
}
],
"image": null
}

你正在使用的Web框架是什么?是快递吗?为 webhook 添加代码会有所帮助,但现在您的签名已关闭。

在这里看到我的答案 Nodejs - Expressjs - Verify shopify webhook 了解如何使用正文解析器的验证方法。

然后你的钩子处理程序将如下所示:

router.post('/webhooks/products/create', (req, res)=>{
console.log(JSON.stringify(req.body, null, ' '));
res.sendStatus(200);
// process the webhook
});

看起来你正在使用基本节点/反应示例中的 Koa 框架。

假设你已经找到了它,但任何看的人,答案是ctx.state.webhook.payload
你可以从那里访问各个元素,所以ctx.state.webhook.payload.title等也有效。

相关内容

  • 没有找到相关文章