title: SOAP API Integration Assistance
body: <p>Good day StackOverflow, I am currently integrating a credit check SOAP API, I was given some sample code without a <em>package.json</em> file, so I have no idea as to the development environment. Here is the sample code I was given:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var request = require('request');
  var fs = require('fs');
  var bsplit = require('buffer-split');
  
  //process.env.http_proxy = 'http://host:port';
  
  var xmlData = '&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webServices/"&gt;' +
      '&lt;soapenv:Header/&gt;' +
      '&lt;soapenv:Body&gt;' +
      '   &lt;web:DoNormalEnquiryStream&gt;' +
      '       &lt;request&gt;' +
      '           &lt;pUsrnme&gt;*****&lt;/pUsrnme&gt;' +
      '           &lt;pPasswrd&gt;*****&lt;/pPasswrd&gt;' +
      '           &lt;pVersion&gt;1.0&lt;/pVersion&gt;' +
      '           &lt;pOrigin&gt;QA-SOAPUI&lt;/pOrigin&gt;' +
      '           &lt;pOrigin_Version&gt;1.0&lt;/pOrigin_Version&gt;' +
      '           &lt;pInput_Format&gt;XML&lt;/pInput_Format&gt;' +
      '           &lt;pTransaction&gt;' +
      '               &lt;![CDATA[&lt;Transactions&gt;' +
      '                   &lt;Search_Criteria&gt;' +
      '                       &lt;CS_Data&gt;Y&lt;/CS_Data&gt;' +
      '                       &lt;CPA_Plus_NLR_Data&gt;Y&lt;/CPA_Plus_NLR_Data&gt;' +
      '                       &lt;Deeds_Data&gt;N&lt;/Deeds_Data&gt;' +
      '                       &lt;Directors_Data&gt;N&lt;/Directors_Data&gt;' +
      '                       &lt;Identity_number&gt;&lt;/Identity_number&gt;' +
      '                       &lt;Surname&gt;&lt;/Surname&gt;' +
      '                       &lt;Forename&gt;&lt;/Forename&gt;' +
      '                       &lt;Forename2/&gt;' +
      '                       &lt;Forename3/&gt;' +
      '                       &lt;Gender&gt;M&lt;/Gender&gt;' +
      '                       &lt;Passport_flag&gt;N&lt;/Passport_flag&gt;' +
      '                       &lt;DateOfBirth&gt;19820914&lt;/DateOfBirth&gt;' +
      '                       &lt;Address1&gt;&lt;/Address1&gt;' +
      '                       &lt;Address2&gt;&lt;/Address2&gt;' +
      '                       &lt;Address3/&gt;&lt;Address4/&gt;' +
      '                       &lt;PostalCode&gt;&lt;/PostalCode&gt;' +
      '                       &lt;HomeTelCode/&gt;' +
      '                       &lt;HomeTelNo/&gt;' +
      '                       &lt;WorkTelCode/&gt;' +
      '                       &lt;WorkTelNo/&gt;' +
      '                       &lt;CellTelNo/&gt;' +
      '                       &lt;ResultType&gt;JPDF2&lt;/ResultType&gt;' +
      '                       &lt;RunCodix&gt;N&lt;/RunCodix&gt;' +
      '                       &lt;Adrs_Mandatory&gt;N&lt;/Adrs_Mandatory&gt;' +
      '                       &lt;Enq_Purpose&gt;12&lt;/Enq_Purpose&gt;' +
      '                       &lt;Run_CompuScore&gt;N&lt;/Run_CompuScore&gt;' +
      '                       &lt;ClientConsent&gt;Y&lt;/ClientConsent&gt;' +
      '                       &lt;/Search_Criteria&gt;' +
      '            &lt;/Transactions&gt;]]&gt;' +
      '          &lt;/pTransaction&gt;' +
      '       &lt;/request&gt;' +
      '   &lt;/web:DoNormalEnquiryStream&gt;' +
      '   &lt;/soapenv:Body&gt;' +
      '   &lt;/soapenv:Envelope&gt;';
  
  request({
      url: "api.myapiurl.com/soap-api",
      encoding:null,
      method: "POST",
      headers: {
          "content-type": "text/xml"
      },
      body: xmlData
  }, function (error, response, body){
  
      var delim = new Buffer('--uuid');
      var result = bsplit(body,delim);
  
      var attBuffs = [];
      var attBuffsStartIndex = [];
  
      for(var i = 0; i &lt; result.length; i++){
          if(result[i].toString().indexOf("Content-Type: application/octet-stream") &gt; 0){
              attBuffs.push(result[i]);
              var trimmedString = result[i].toString();
              attBuffsStartIndex.push(trimmedString.indexOf("Content-Transfer-Encoding: binary")+37);
          }
      }
  
      fs.open('JsonFile.json', 'w', function (err, fd) {
          if (err) {
              throw 'error opening file: ' + err;
          }
  
          fs.write(fd, attBuffs[0], attBuffsStartIndex[0], attBuffs[0].length - attBuffsStartIndex[0], null, function (err) {
              if (err) throw 'error writing file: ' + err;
              fs.close(fd, function () {
                  console.log('JSON File written to file');
              })
          });
      });
  
      fs.open('PdfFile.pdf', 'w', function (err, fd) {
          if (err) {
              throw 'error opening file: ' + err;
          }
  
          fs.write(fd, attBuffs[1], attBuffsStartIndex[1], attBuffs[1].length - attBuffsStartIndex[1], null, function (err) {
              if (err) throw 'error writing file: ' + err;
              fs.close(fd, function () {
                  console.log('PDF File written to file');
              })
          });
      });
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>I am having some difficulty getting this code to run. I thought it might need the [RequireJS API], but after doing some research I strongly believe it's actually the <a href="https://github.com/request/request" rel="nofollow noreferrer">Request HTTP Client</a>. I really struggling to get this sample code running. Any assistance would be greatly appreciated!</p>
  
  <hr>
  
  <p><strong>Addition</strong>
  I am aware that this is it requires nodejs. But I have not been able to figure out how to progress from there.</p>
  
  <hr>
  
  <p><strong>Addition</strong></p>
  
  <p>If anyone is interested native node modules such as <em>net, fs etc</em> are not meant to be run from a browser, so this code will not work anyway.</p>