【问题标题】:Error occured Invalid payload provided. No JSON object could be decoded Coinbas发生错误 提供的有效负载无效。没有 JSON 对象可以被解码 Coinbase
【发布时间】:2021-08-30 13:20:00
【问题描述】:

我已经与这个错误作斗争数周了,阅读了数百个刚刚被证明是徒劳的资源,并联系了 coinbase 支持,但没有得到任何回应。当我尝试从 coinbase Api 测试 webhook 时,我收到此错误“位于 07b0a16c5735.ngrok.io 的远程服务器返回 HTTP 400”,在我的控制台中我收到“发生错误,提供的有效负载无效。无法解码 JSON 对象”

我附上了我遇到的错误的屏幕截图,这是我的端点代码;

app.post(
  "/endpoint", (request, response) => {
    var event;

    console.log(request.headers);

    try {
      event = Webhook.verifyEventBody(
        request.rawBody,
        request.headers["x-cc-webhook-signature"],
        webhookSecret
      );
    } catch (error) {
      console.log("Error occured", error.message);

      return response.status(400).send("Webhook Error:" + error.message);
    }

    console.log("Success", event.id);
    console.log(request.rawBody);

    response.status(200).send("Signed Webhook Received: " + event.id);
  }
);

我目前正在使用这行代码将原始正文解析为json:

function rawBody(req, res, next) {
  req.setEncoding("utf8");

  var data = "";

  req.on("data", function (chunk) {
    data += chunk;
  });

  req.on("end", function () {
    req.rawBody = data;

    next();
  });
}

app.use(rawBody);

我尝试使用 express.json(),但是当我仍然遇到相同的错误时,为上面的行代码补充了它。这是我期待的回应:

Success 00000000-0000-0000-0000-000000000000
{"id":"00000000-0000-0000-0000-000000000000","scheduled_for":"2018-01-01T00:40:00Z","attempt_number":1,"event":{"id":"00000000-0000-0000-0000-000000000000","resource":"event","type":"charge:confirmed","api_version":"2018-03-22","created_at":"2018-01-01T00:40:00Z","data":{"code":"AAAAAAAA","id":"00000000-0000-0000-0000-000000000000","resource":"charge","name":"The Sovereign Individual","description":"Mastering the Transition to the Information Age","hosted_url":"https://commerce.coinbase.com/charges/AAAAAAAA","created_at":"2018-01-01T00:00:00Z","confirmed_at":"2018-01-01T00:40:00Z","expires_at":"2018-01-01T01:00:00Z","support_email":"test@test.test","timeline":[{"time":"2018-01-01T00:00:00Z","status":"NEW"},{"status":"PENDING","payment":{"network":"ethereum","transaction_id":"0x0000000000000000000000000000000000000000000000000000000000000000"},"time":"2018-01-01T00:30:00Z"},{"status":"COMPLETED","payment":{"network":"ethereum","transaction_id":"0x0000000000000000000000000000000000000000000000000000000000000000"},"time":"2018-01-01T00:40:00Z"}],"metadata":{},"payment_threshold":{"overpayment_absolute_threshold":{"amount":"15.00","currency":"USD"},"overpayment_relative_threshold":"0.1","underpayment_absolute_threshold":{"amount":"5.00","currency":"USD"},"underpayment_relative_threshold":"0.1"},"pricing":{"local":{"amount":"100.00","currency":"USD"},"bitcoin":{"amount":"1.00000000","currency":"BTC"},"usdc":{"amount":"10.000000","currency":"USDC"},"bitcoincash":{"amount":"5.00000000","currency":"BCH"},"litecoin":{"amount":"2.00000000","currency":"LTC"}},"pricing_type":"fixed_price","payments":[{"network":"ethereum","transaction_id":"0x0000000000000000000000000000000000000000000000000000000000000000","status":"CONFIRMED","detected_at":"2018-01-01T00:30:00Z","value":{"local":{"amount":"100.0","currency":"USD"},"crypto":{"amount":"10.00","currency":"ETH"}},"block":{"height":100,"hash":"0x0000000000000000000000000000000000000000000000000000000000000000","confirmations_accumulated":8,"confirmations_required":2}}],"addresses":{"bitcoin":"1000000000000000000000000000000000","usdc":"0x0000000000000000000000000000000000000000","litecoin":"3000000000000000000000000000000000","bitcoincash":"bitcoincash:000000000000000000000000000000000000000000"},"exchange_rates":{"BCH-USD":"1000.0","BTC-USD":"100.0","ETH-USD":"10.0","JPY-USD":"0.5","LTC-USD":"50.0","TST-USD":"0.5","BEER-USD":"0.1"}}}}

【问题讨论】:

  • 你是如何到达终点的?什么是有效载荷?
  • Coinbase commerce 向端点发送响应

标签: node.js json webhooks rest coinbase-api


【解决方案1】:

我有同样的问题。 对我有用的是将处理 webhook (coinbase-commerce-node) 中的有效负载的 npm 包的版本降级到版本 1.0.0。 我还完全按照库示例中所示实现了 webhook: https://github.com/coinbase/coinbase-commerce-node/tree/master/examples/webhook

您还需要确保您的服务器已正确配置为使用 SSL。例如,如果您在 Heroku 中实现了 webhook,则可以使用 heroku-ssl-redirect 来强制使用 SSL。

【讨论】:

    猜你喜欢
    • 2018-02-20
    • 2021-06-01
    • 1970-01-01
    • 2020-06-13
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    相关资源
    最近更新 更多