0

With the below code I am trying to get some basic pull request info from my Github repo, when someone submits a pull request for it. But I get this output instead:

$ node poc2.js 
sha1=fef5611617bf56a36d165f73d41e527ee2c97d49
res [Circular]
req [Circular]

Since the secret hash is printed, the webhook is received. I can also verifiy this by nc -l 8080 instead of running my NodeJS app. There is will see a large json object, which is from where I got the json structure I use in the console.log's below.

Question

Can anyone figure out, why I get a "circular" json object from both req and res?

And how can I get the values I console.log?

const secret = "sdfsfsfsfwerwergdgv";

const http = require('http');
const crypto = require('crypto');

http.createServer(function (req, res) {
    req.on('data', function(chunk) {
        let sig = "sha1=" + crypto.createHmac('sha1', secret).update(chunk.toString()).digest('hex');
        console.log(sig);
        if (req.headers['x-hub-signature'] == sig) {
          console.log("res %j", res);
          console.log("req %j", req);
          if (res.action == 'opened') {

            console.log('PR for: ' + res.repository.html_url);
            console.log('PR for: ' + res.repository.full_name);

            console.log('PR: ' + res.pull_request.html_url);
            console.log('PR title: ' + res.pull_request.title);
            console.log('PR description' + res.pull_request.description);
            console.log('PR by user: ' + res.pull_request.user.login);
          };
        }
    });

    res.end();
}).listen(8080);
1
  • 2
    Strictly speaking, there cannot possibly be a "circular JSON object:". There can however be JavaScript object graphs involving circular references.
    – Pointy
    Commented Feb 15, 2021 at 22:58

1 Answer 1

3

The req is circular because it's not a JSON object, it's the Request object which includes a lot of internal machinery, some of which is circularly linked.

The actual incoming JSON document is in req.body or wherever your particular JSON body parser puts it, though in this example you don't have one yet so that's something you should fix.

Tip: You may want to use Express instead of the core Node http module, it's much more capable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.