Get Data of Webhook notify

I signed up to receive the notifications but when I receive the answers I do not receive the object as shown in the api and yes these answers. Someone knows what may be wrong.

Solicitação:

var options = {
    method: 'GET',
    uri: "https://api.twitch.tv/helix/webhooks/subscriptions?first=10",
    json: {
        'hub.callback': 'http://65465454a.ngrok.io',
        'hub.mode': 'subscribe',
        'hub.topic': 'https://api.twitch.tv/helix/streams?user_id=<Client ID>',
        'hub.lease_seconds': "864000",
        'hub.secret': '<Secret>'
    },
    headers: {
        'Authorization': 'Bearer Auth'
    }
}

const req2 = request(request2, (err, res, body) => {
    console.log('Resp Request 2')
    !err ? '' : console.log('Erro: ' + err)
    !res ? '' : console.log('res: ' + res.statusCode + ' ' +res.statusMessage)
    !body ? '' : console.log('body: ' + JSON.stringify(body))
    console.log('=================================================================')
  })

var count = 0

app.post('*', (req, res) => { 
    count++
    console.log(count)
    console.log('Req Query: ' + JSON.stringify(req.query) )
    console.log('Req Headers: ' + JSON.stringify(req.headers))

    console.log('Respostas:')
    console.log(req.body)
    console.log('==============================================================================')
    res.sendStatus(200)
})

app.post('/api/twitch/callback', (req,res) => {
    console.log('post callback', JSON.stringify(req))
    response.sendStatus(200)
})

app.get('/', (req, res) =>{
    res.sendFile(__dirname+'/index.html')
    if(req.query['hub.topic'] !== (null || undefined)){
        console.log(req.query['hub.topic'])
        var challenge = req.query['hub.challenge']
        console.log(challenge)
        res.send(challenge)
    }
})

Resposta:

Req Headers: {"host":"54645654.ngrok.io","user-agent":"Go-http-client/1.1","connection":"close","content-length":"361","content-type":"application/json; charset=utf-8","link":"<https://api.twitch.tv/helix/webhooks/hub>; rel=\"hub\", <https://api.twitch.tv/helix/streams?user_id=1377>; rel=\"self\"","twitch-notification-id":"9d48e515-0134-5cba-81d6-b05077c95429","twitch-notification-timestamp":"2019-05-02T22:52:45Z","x-hub-signature":"sha256=1b428fb135877550150fde58231d0d743a75441ebd085884133124970bcb8770","accept-encoding":"gzip","x-forwarded-proto":"https","x-forwarded-for":"34.218.218.240"}

Are you using Express for as your web server, and are you using middleware such as body-parser to properly handle the body of incoming requests?

If it is Express that you’re using then you need to ensure you use body-parser or else req.body will be undefined.

Here’s an example of how to use body parser to both make the body of webhook notifications be assigned to req.body, and also perform the verification step.

const bodyParser = require('body-parser');
const crypto = require('crypto');

const verifyNotice = (req, res, buf, encoding) => {
    const expected = req.headers['x-hub-signature'];
    const calculated = 'sha256=' + crypto.createHmac('sha256',  webhookSecret).update(buf).digest('hex');
    req.verified = expected === calculated;
};

app.use(bodyParser.json({ verify: verifyNotice }));

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.