coinbase-commerce-node
coinbase-commerce-node copied to clipboard
Error occured. Invalid payload provided. No JSON object could be decoded.
I have this error when I send a test.
app.post('/pay/crypto/charge', rate, async (req, res) => {
const webhookSecret = 'my-secret';
var event;
console.log(req.headers);
try {
event = Webhook.verifyEventBody(
req.rawBody,
req.headers['x-cc-webhook-signature'],
webhookSecret
);
console.log(event);
} catch (error) {
console.log('Error occured', error.message);
return res.status(400).send('Webhook Error:' + error.message);
}
})
Hello @MrlolDev If you are using express, probably the issue comes from bodyParser or express.json() middleware. You could add this to solve the issue with the rawBody.
app.use(
express.json({
verify: (req, res, buf) => {
req.rawBody = buf;
},
})
);
You can find a more detailed explanation here: https://flaviocopes.com/express-get-raw-body/
And this is my repo using this package: https://github.com/FaztWeb/nodejs-coibase-commerce
I had same issue and I am able to fix it after using JSON.stringify(req.body)
instead of req.rawBody
. Something like this
app.post("/webhook", async (req, res) => { try { const event = Webhook.verifyEventBody( JSON.stringify(req.body), req.headers['x-cc-webhook-signature'], process.env.COINBASE_WEBHOOK_SECRET ); console.log(event); } catch (error) { res.status(400).json({ error }); } })