Webhook hub.challenge query

Hi, I am able to send a POST request to twitch which then responds to my callback url (http listen server) I receive data from twitch which I can log to console, but I cannot figure out how to separate the “hub.challenge” string to print to console, which I would then send back to twitch with something like response.send(request.query['hub.challenge'])

My Listen server code is as follows:

const http = require(“http”);
const port = 3001;
const server = http.createServer();

console.log(“Booting up listen server”);

server.on(“request”, function(request, response){
//response.writeHead(200); //replies with resonse code 200 success
console.log("Method: " + request.method);
console.log("Headers: " + request.headers);
console.log("URL: " + request.url);

var challenge = request.query[“hub.challenge”];
console.log(challenge: ${challenge});

var data = “”;
//var challenge = “”;
request.on(“data”, function(chunk){
data += chunk.toString();
//no data comes from twitch
});

request.on(“end”, function(){
console.log(The data collected is:\n ${data});
//data is blank if from twitch
console.log(“Awaiting next request…”);
response.end();
});

});

server.listen(port);

console.log(“Listen server created at http://MYIPADDRESS” + port);

and response I receive with

Booting up listen server
Listen server created at http://MYIPADDRESS:3001
Method: GET
Headers: [object Object]
URL: /?hub.challenge=zK9bELyefWu8ELPRwZrtq35TLrwU7ktwLkcMC7r2&hub.lease_seconds=5&hub.mode=subscribe&hub.topic=https%3A%2F%2Fapi.twitch.tv%2Fhelix%2Fstreams%3Fuser_id%3D5678
/home/todgins/Lillette/callback.js:15
var challenge = request.query[“hub.challenge”];
^

TypeError: Cannot read property ‘hub.challenge’ of undefined

console response as a image link: http://puu.sh/CByzt.png

I am unsure how to grab the “hub.challenge” string.

If I grab this and the send it back from the listen server does that complete my handshake for a subscription? I just need to execute the initial request once per lease time. (It’s currently set to 5 seconds while testing)

Thanks!

Hopefully I got the code box formatting right…

EDIT: Key info, I am using Node.js

Do yourself a favour and switch to using Express which will provide you a better structure for mucking about with inbound requests.

Here you have not processed the request.query into an array to use.

You can use URL to do this

for example

const url = require('url');

var parts = url.parse(request.url);
var challenge = parts.query[“hub.challenge”];
console.log( `challenge: ${challenge}` );

Should suffice. But express will save you time and effort

Thanks for the quick reply!

Adding these stop the crashing but hub.challenge is still returned as undefined in the console.log.

EDIT:

var parts = url.parse(request.url, true);

As per the link you provided results in the expected string printing to console!

I will check out/read up on express next chance I get!

1 Like

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