Wednesday, 26 April 2017

Can not POST using https request in node.js

I want to send the request using https in node.js. Here I try my code for send https request but it gives me error

Cannot POST /v1/getdetails

How to solve this error?

var express      = require('express');
var app         = express();
var port        = process.env.PORT || 3000;
var https = require('https');

 app.get('/', function(req, res) {
    var data = JSON.stringify({
            channelid: 50
        });

        var ntoptions = {
            host: 'www.google.com',
            path: '/v1/getdetails',
            port: 443,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Content-Length': Buffer.byteLength(data)
            }
        };

        var httpsreq = https.request(ntoptions);

        httpsreq.write(data);
        httpsreq.end();

        httpsreq.on('error', function (err) {
            console.log('Error ' + err );
            return cb(err);
        });

        var responseData = '';
        httpsreq.on('response', function(httpsres){
            httpsres.on('data', function(chunk){
                responseData += chunk;
            });

            httpsres.on('end', function(){

                console.log(responseData);
                return;
            });
         });
 });

app.listen(port);
console.log('Server started at http://localhost:' + port);

It gives me error like

Cannot POST /v1/getdetails

How to solve this error?



via Brothers

No comments:

Post a Comment