Friday, 28 December 2018

Node.js http post request with basic authentication

I can make this work with axios but as I want to do this with default http module for some reasons

Here is the code

var express = require("express");
const http = require('https');
var app = express();

app.listen(3000, function () {
    var username = 'username';
    var password = 'password';
    var auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');

const data = JSON.stringify({
    campaign_id: 'all',
    start_date: '01/01/2010',
    end_date: '05/31/2030',
    return_type: 'caller_view',
    criteria: {
        phone: 98855964562
    }
});

var hostName = "https://staging.crm.com";
var path = "/api/v1/caller_find";

const options = {
    hostName: hostName,
    path: path,
    port: 3000,
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Authorization': auth,
        'Content-Length': data.length
    }
};
const req = http.request(options, (res) => {
    console.log('response is ' + res);
});

req.on('error', (error) => {
    console.log('error is ' + error);
  });
});

But it is throwing exception

connect ECONNREFUSED 127.0.0.1:443



from Node.js http post request with basic authentication

No comments:

Post a Comment