Verify Webhook
We make use of Svix to send and manage our webhooks infrastructure and have linked the relevant link below. You can also find an example of what verification looks like below.
const { Webhook } = require('svix');
const bodyParser = require('body-parser');
app.post(
'/v1/hook',
bodyParser.raw({ type: 'application/json' }),
(req, res) => {
res.json({});
const payload = req.body;
const headers = req.headers;
const wh = new Webhook(secret);
let msg;
try {
msg = wh.verify(payload, headers);
console.log(msg);
// Do something with the message...
} catch (err) {
console.log(err);
return;
}
}
);
http.HandleFunc("/webhook", func(w http.ResponseWriter, r *http.Request) {
headers := r.Header
// var b = new(bytes.Buffer)
// _,err := io.Copy(b,r.Body)
payload, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// Make sure you can reuse payload to process the request
// like retrieving details about the request body
// json.NewDecoder(r.Body).Decode(struct)
r.Body = ioutil.NopCloser(bytes.NewBuffer(b))
err = wh.Verify(payload, headers)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// Do something with the message...
w.WriteHeader(http.StatusNoContent)
})
Updated about 1 year ago