dpigera
dpigera

Reputation: 3369

Node.js HTTPS POST Request at IP Address fails

I'm trying to make an HTTPS POST SOAP Request using the following script:

var http = require('http');

function Authenticate() {
  // Build the post string from an object
  var post_data = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:typ=\"http://company.com/mse/types\"><soapenv:Header/><soapenv:Body><typ:Login><typ:Credential userName=\"username\" password=\"password\"/></typ:Login></soapenv:Body></soapenv:Envelope>";
  // An object of options to indicate where to post to

  var post_options = {
    port: 443,
    host: 'https://132.33.223.33', //MSE12
    path: '/aaa/',
    method: 'POST',

    headers: {
        'Content-Type': 'text/xml;charset=UTF-8',
        'SOAPAction': '\"Login\"',
        'Accept-Encoding': 'gzip,deflate',
        'Host':'173.37.206.33',
        'Connection':'Keep-Alive',
        'User-Agent':'Apache-HttpClient/4.1.1 (java 1.5)'
    }
  };

  // Set up the request
  var post_req = http.request(post_options, function(res) {
      res.setEncoding('utf8');
      res.on('data', function (chunk) {
          console.log('Response: ' + chunk);
      });
  });

  post_req.on('error', function (err) {
    //handle error here
    console.log(err);
  });

  // post the data
  post_req.write(post_data);
  post_req.end();

}

Authenticate();

IPAddress and payload has been changed. I know the request works successfully, as the Headers, request body and request url work correctly in the F

When executed, I keep running into this error:

{ [Error: getaddrinfo ENOTFOUND] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo' }

Can anyone see what I'm doing wrong in this script while making this HTTPS request? I tried a search on this error message and it keeps pointing to forum threads about an issue with the host.

Any pointers will be greatly appreciated.

Upvotes: 0

Views: 4429

Answers (1)

spotirca
spotirca

Reputation: 471

I can see here 2 problems:

First: you're trying to use https, not http. Therefore you should use

var https = require( 'https' );

and from then on, use https instead of http. See Node HTTPS for more info. Second, in *post_options*, the host parameter should be only the host, without the schema. So let host: 132.33.223.33. That's why you get the "ENOTFOUND" error.

Upvotes: 6

Related Questions