找不到来自Shopify Webhook的数据/有效载荷

时间:2019-08-30 15:26:08

标签: javascript node.js shopify webhooks

摘要

我一直在关注有关使用Node和React设置应用程序的Shopify教程。我的所有工作都在进行中,但我正在努力超越最后一步。我正在尝试从Shopify Webhooks返回数据,但看不到数据/有效负载在哪里。

背景

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

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

预期产量

我希望收到Shopify Webhook事件文档中所述的json对象。例如

{
  "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
}

2 个答案:

答案 0 :(得分:1)

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

Nodejs - Expressjs - Verify shopify webhook处查看我的答案,了解如何使用主体解析器的verify方法。

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

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

答案 1 :(得分:0)

您似乎在使用基本节点/反应示例中的Koa框架。

假设您已经找到它,但是正在寻找的人,答案是ctx.state.webhook.payload
您可以从那里访问各个元素,以便 ctx.state.webhook.payload.title等也可以。