Saturday 18 February 2017

Newbie: http server help.

Besides a hello world example, this is my fist attempt at node. I'm trying to console out webhook callbacks from an API. I got the PUT request to display how I want them, but after a PUT request I need to make a GET request and I would like to display the response in the console too.Edit: Here's what I'm trying to accomplish. Ok, so I have a webhook I created, this webhook sends a POST request to my server every time a message is posted. This POST request will have an ID. I need to get that ID and then make a GET request using that ID. I would like to print out the contents of the POST request and GET response to the console. Does this help?Here's what I have so far:var http = require("http"); var port = 8080; //setup future GET request var options = { url: 'http://ift.tt/2lY4FTk', method: 'GET', headers: { 'User-Agent': 'request', 'Authorization': 'Bearer bar' } } var server = http.createServer(); server.on('request', function(request, response) { console.log(request.method); if(request.method == 'POST'){ var jsonString = ''; request.on('data', function(data){ jsonString += data; }); request.on('end', function(){ console.log(JSON.parse(jsonString)); post = JSON.parse(jsonString); //make GET request http.request(options).end(); }); } if(request.method == 'GET'){ var jsonString1 = ''; response.on('data', function(chunk){ jsonString1 += chunk; }); request.on('end', function(){ console.log((jsonString1)); }); } response.end(); }); server.listen(port); console.log('Online'); Anyone have any ideas or examples I could follow?

Submitted February 18, 2017 at 03:42PM by DAM9779

No comments:

Post a Comment