0

I am trying to store the return value of https request into a variable (var a) to use it later in my later steps.
(I should use https - standard node module)

function createPNR(pnrdate){
  return new Promise(async function (resolve, reject){
    process.env.NODE_TLS_REJECT_UNAUTHORIZED='0'
    const https = require('https');
    var pnrData='';
    var responseJSON=''
    var postData = JSON.stringify({     "enivironment": "UAT",     "date": pnrdate });
    var options = {
      hostname: 'my.TDM.server.dummy',
      port: 443,
      path: '/my-api/tdm/method/abcd',
      method: 'POST',
      headers: {
           'Content-Type': 'application/json',
           'Content-Length': postData.length,
         }
    };
    var req = https.request(options, (res) => {
      console.log('statusCode:', res.statusCode);
      console.log('headers:', res.headers);
      res.on('data', (d) => {
        // process.stdout.write("the response is::   "+d);
        responseJSON=JSON.parse(d) //Details[0].pnrNumber
        pnrData = responseJSON.Details[0].pnrNumber
        console.log('pnrData is: '+pnrData);
      });
    });
    req.on('error', (e) => {
      console.error(e);
    });
    req.write(postData);
    req.end();
    return pnrData    
  })
    
  }

  const printPNR = async () => {
    console.log("PRINT ADDRESS");
    var a = await createPNR('10APR2023');
    console.log('the created PNR is: '+a);
  };
  
  console.log("RUN");
  printPNR();

When I run this code, the createPNR methods gets executed, however the value is not getting stored in variable a and console.log statement it not printed at all.
Appreciate if someone can help me on this.

deceze
  • 510,633
  • 85
  • 743
  • 889
anees
  • 13
  • 4

0 Answers0