title: Socket Hang Up { [Error: socket hang up] code: 'ECONNRESET' }
body: <p>I want to hit API in <code>Node.js</code> for connecting with SOAP and HTTPS. When I hit the URL for performing function with HTTPS request, I used the following code for connection with SOAP Server for performing data transfer. But it is showing an error:</p>
  
  <blockquote>
    <p>{ [Error: socket hang up] code: 'ECONNRESET' }:</p>
  </blockquote>
  
  <pre><code>exports.check=function(req,res) {
      //Add SSL to communicate with SOAP
      var sslRootCAs = require('ssl-root-cas/latest')
          .inject()
          .addFile( __dirname + '/Symantec Class 3 Secure Server CA - G4.cer' );
  
      var body = '&lt;?xml version="1.0" encoding="UTF-8"?&gt;' +
          '&lt;!—Worked Example 1: Birth Request --&gt;' +
          '&lt;RegBirths SchemaVersion="1.0" ProgramName="CTWSProg" ProgramVersion="1b" RequestTimeStamp="2001-12-17T09:30:47-05:00" &gt;' +
              '&lt;Authentication&gt;' +
                  '&lt;CTS_OL_User Usr="111-111-111" Pwd="m0nster"/&gt;' +
              '&lt;/Authentication&gt;' +
              '&lt;Births TxnId="123889"&gt;' +
                  '&lt;Birth RowNum="1" Etg="UK520202500138" Dob="2004-03-01" Brd="HF" Sex="f" EId="111222333" GdEtg="UK560002400063"' +
                      'BLoc="20/002/0001" PLoc="01/001/0001" PSLoc="04" IWarn="n"/&gt;' +
                  '&lt;Birth RowNum="2" Etg="UK240001300777" Dob="2004-03-09" Brd="HF" Sex="f" EId="111222333" GdEtg="UK560002100018"' +
                      'BLoc="20/002/0001" PLoc="01/001/0001" PSLoc="04" IWarn="n" /&gt;' +
              '&lt;/Births&gt;' +
          '&lt;/RegBirths&gt;';
  
      var body2 = '&lt;?xml version="1.0" encoding="utf-8"?&gt;'+
          '&lt;soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;' +
              '&lt;soap:Body&gt;' +
                  '&lt;TransferData xmlns="http://www.defra.gov.uk"&gt;' +
                      '&lt;username&gt;' + username + '&lt;/username&gt;' +
                      '&lt;password&gt;' + password + '&lt;/password&gt;' +
                      '&lt;serviceName&gt;DEFRA-CTWS-FULL-PROVING&lt;/serviceName&gt;' +
                      '&lt;data&gt;'+ body +'&lt;/data&gt;' +
                      '&lt;type&gt;Register_Births_Asynchronous-V1-0&lt;/type&gt;' +
                  '&lt;/TransferData&gt;' +
              '&lt;/soap:Body&gt;' +
          '&lt;/soap:Envelope&gt;';
  
      var postRequest = {
          host : "hostman.com",
          path: "/abcxyz.asmx?WSDL",
          port: 443,
          rejectUnauthorized: true,
          method: "POST",
          headers: {
              'Accept': 'application/xml',
              'Content-Type': 'application/xml; charset=UTF-8',
              'SOAPAction': "https://www.axys.com/TransferData",
              'Content-Length': Buffer.byteLength(body2)
          }
      };
  
      var buffer = "";
  
      var req = http.request( postRequest, function( res ) {
          console.log( res );
          var buffer = "";
          res.on( "data", function( data ) { buffer = buffer + data; } );
          res.on( "end", function( data ) { console.log( buffer ); } );
          res.on('error', function (err) { console.log( "----ERROR----" ); console.log(err);});
      });
      // req.on('error', function(err) {
      //     console.log( "----ERROR----" ); console.log(err);
      // });
      // `req`.on('socket', function (socket) {
      //   console.log(socket)
      //     socket.setTimeout(3000);  
      //     socket.on('timeout', function() {
      //         req.abort();
      //     });
      // });
      req.on('error', function(err) {
          if (err.code === "ECONNRESET") {
              console.log("Timeout occurs");
              //specific error treatment
          }
      });
      req.write( body );
      req.end();
  }
  </code></pre>