【问题标题】:POSTing to a url in Node (working with webhooks)发布到 Node 中的 url(使用 webhook)
【发布时间】:2022-08-19 03:45:30
【问题描述】:

我正在做一个项目,我正在使用 Shippo 创建运输标签。 Shippo 有一个 webhook,允许客户跟踪他们的订单状态。我以前从未使用过 webhook,而且我发现他们关于如何设置它的文档有点令人困惑。他们说:

  1. 设置网络钩子。
  2. POST 以下为https://api.goshippo.com/tracks/
    {
        \"carrier\": \"usps\",
        \"tracking_number\": \"9102969010383081813033\"
    }
    

    我遇到的问题是我不确定如何 POST 到 node.js 中的 url。我的项目在前端使用 react 并在后端使用节点,并使用 axios 在两者之间发送我的 api 请求。

    我找到了这个posting to a remote url with expressjs,但答案是从 2013 年开始的,所以我不确定从那以后是否有任何变化,而且总的来说,我不确定这个解决方案是否适用于我\'我问。我将不胜感激有关如何完成此任务的任何帮助或建议。谢谢!

标签: node.js shippo


【解决方案1】:

您可以使用 axios 模块中的 post() 函数发布到端点。 句法:

const axios = require("axios"); // Commonjs
axios.post( // Send POST request
    "https://api.goshippo.com/tracks/" /*URL*/,
    JSON.stringify({"carrier": "usps","tracking_number": "9102969010383081813033"}) /*body*/,
    {
        "headers": {
            "content-type":"application/json", /*indicate that the body is of type json*/
            "authorization": "ShippoToken <API_TOKEN>" /*Your api key*/
        }
    }
).then((response)=>{
    console.log(response.data); // The response in object form
    // Do what you want with the data
}).catch((error)=>{
    console.error(error); // The request failed
})

编辑:更改代码以包含 api 密钥

【讨论】:

    【解决方案2】:

    我使用 nodejs 为 Shippo 构建了 webhook 演示。你可以在这里找到它。请参考那个,让我知道这是否是你要找的。

    https://github.com/vyshakhbabji/shippo-webhook-demo

    【讨论】:

      猜你喜欢
      • 2020-04-18
      • 1970-01-01
      • 2023-02-17
      • 2014-10-15
      • 1970-01-01
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多