admin管理员组

文章数量:1404564

I am trying to make a SOAP call through node.js and am getting the below error:

ReferenceError: $http is not defined

Here is my code, everything else appears to work until it fails at the last line:

//Import the `assert` module to validate results.
var assert = require('assert');
var SoapRequestXML='<soapenv:Envelope xmlns:soapenv="/" xmlns:_5="">\r\n' +
                   '<soapenv:Header/>\r\n' +
                   '<soapenv:Body>\r\n' +
                   '<_5:GetClasses>\r\n' +
                   '<_5:Request>\r\n' +
                   '<_5:SourceCredentials>\r\n' +
                   '<_5:SourceName>SOURCECREDENTIALS</_5:SourceName>\r\n' +
                   '<_5:Password>PASSWORD</_5:Password>\r\n' +
                   '<_5:SiteIDs>\r\n' +
                   '<_5:int>-99</_5:int>\r\n' +
                   '</_5:SiteIDs>\r\n' +
                   '</_5:SourceCredentials>\r\n' +
                   '</_5:Request>\r\n' +
                   '</_5:GetClasses>\r\n' +
                   '</soapenv:Body>\r\n' +
                   '</soap:Envelope>';


var options = {
    //Define endpoint URL.
    url: ".asmx",
    //Define body of POST request.
    body: SoapRequestXML,
    //Define insert key and expected data type.
    headers: {
         'POST': '.asmx?wsdl HTTP/1.1',
         'Accept-Encoding': 'gzip,deflate',
         'Content-Type': 'text/xml;charset=UTF-8',
         'SOAPAction': '""',
         'Content-Length': '594',
         'Host': 'api.mindbodyonline',
         'Connection': 'Keep-Alive',
         'User-Agent': 'Apache-HttpClient/4.1.1 (java 1.5)'


        }
};

//Define expected results using callback function.
function callback(error, response, body) {
    //Log status code to Synthetics console.
    console.log(response);
    //Verify endpoint returns 200 (OK) response code.
    assert.ok(response.statusCode == 200, 'Expected 200 OK response');
    //Parse JSON received from Insights into variable.
    //
   var parseString = require('xml2js').parseString;
   var XMLReSULT = response.body;
   parseString(XMLReSULT, function (err, result) {
    console.dir(result);

   });

    //Log end of script.
    console.log("End reached");
}

//Make GET request, passing in options and callback.
$http.post(options, callback);

Any help is appreciated, I am very new at this.

I am trying to make a SOAP call through node.js and am getting the below error:

ReferenceError: $http is not defined

Here is my code, everything else appears to work until it fails at the last line:

//Import the `assert` module to validate results.
var assert = require('assert');
var SoapRequestXML='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap/soap/envelope/" xmlns:_5="https://clients.mindbodyonline./api/0_5">\r\n' +
                   '<soapenv:Header/>\r\n' +
                   '<soapenv:Body>\r\n' +
                   '<_5:GetClasses>\r\n' +
                   '<_5:Request>\r\n' +
                   '<_5:SourceCredentials>\r\n' +
                   '<_5:SourceName>SOURCECREDENTIALS</_5:SourceName>\r\n' +
                   '<_5:Password>PASSWORD</_5:Password>\r\n' +
                   '<_5:SiteIDs>\r\n' +
                   '<_5:int>-99</_5:int>\r\n' +
                   '</_5:SiteIDs>\r\n' +
                   '</_5:SourceCredentials>\r\n' +
                   '</_5:Request>\r\n' +
                   '</_5:GetClasses>\r\n' +
                   '</soapenv:Body>\r\n' +
                   '</soap:Envelope>';


var options = {
    //Define endpoint URL.
    url: "https://api.mindbodyonline./0_5/ClassService.asmx",
    //Define body of POST request.
    body: SoapRequestXML,
    //Define insert key and expected data type.
    headers: {
         'POST': 'https://api.mindbodyonline./0_5/ClassService.asmx?wsdl HTTP/1.1',
         'Accept-Encoding': 'gzip,deflate',
         'Content-Type': 'text/xml;charset=UTF-8',
         'SOAPAction': '"http://clients.mindbodyonline./api/0_5/GetClasses"',
         'Content-Length': '594',
         'Host': 'api.mindbodyonline.',
         'Connection': 'Keep-Alive',
         'User-Agent': 'Apache-HttpClient/4.1.1 (java 1.5)'


        }
};

//Define expected results using callback function.
function callback(error, response, body) {
    //Log status code to Synthetics console.
    console.log(response);
    //Verify endpoint returns 200 (OK) response code.
    assert.ok(response.statusCode == 200, 'Expected 200 OK response');
    //Parse JSON received from Insights into variable.
    //
   var parseString = require('xml2js').parseString;
   var XMLReSULT = response.body;
   parseString(XMLReSULT, function (err, result) {
    console.dir(result);

   });

    //Log end of script.
    console.log("End reached");
}

//Make GET request, passing in options and callback.
$http.post(options, callback);

Any help is appreciated, I am very new at this.

Share Improve this question asked Feb 13, 2017 at 18:31 EvanEvan 231 gold badge1 silver badge3 bronze badges 4
  • 4 Did you import http module before using it let $http = require('http'); ? – Sushanth -- Commented Feb 13, 2017 at 18:34
  • Nothing shown would indicate why it should be defined – charlietfl Commented Feb 13, 2017 at 18:35
  • @Sushanth-- promote that ment to an answer. The OP has apparently included every single line of his code, including other requires, and the necessary one is not among them. – Michael Lorton Commented Feb 13, 2017 at 18:38
  • It almost looks like you are trying to use angular's '$http' service. NodeJS's 'http' module is a bit more low-level than what you're trying to do. You might want to try the 'request' npm package first before refining your question: npmjs./package/request – Steve Hynding Commented Feb 13, 2017 at 18:59
Add a ment  | 

1 Answer 1

Reset to default 1

Like previous ments implied, $http is not defined, and you cant use var $http = require('http') because nodes http does not have a post method. You will need to do a little refactoring. I think you're looking for something like this.

var assert = require('assert')
var http = require('http')
var parseString = require('xml2js').parseString;

var SoapRequestXML='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap/soap/envelope/" xmlns:_5="https://clients.mindbodyonline./api/0_5">\r\n' +
                   '<soapenv:Header/>\r\n' +
                   '<soapenv:Body>\r\n' +
                   '<_5:GetClasses>\r\n' +
                   '<_5:Request>\r\n' +
                   '<_5:SourceCredentials>\r\n' +
                   '<_5:SourceName>SOURCECREDENTIALS</_5:SourceName>\r\n' +
                   '<_5:Password>PASSWORD</_5:Password>\r\n' +
                   '<_5:SiteIDs>\r\n' +
                   '<_5:int>-99</_5:int>\r\n' +
                   '</_5:SiteIDs>\r\n' +
                   '</_5:SourceCredentials>\r\n' +
                   '</_5:Request>\r\n' +
                   '</_5:GetClasses>\r\n' +
                   '</soapenv:Body>\r\n' +
                   '</soap:Envelope>';

var options = {
  hostname: 'api.mindbodyonline.',
  port: 80,
  path: '/0_5/ClassService.asmx',
  method: 'POST',
  headers: {
         'Accept-Encoding': 'gzip,deflate',
         'Content-Type': 'text/xml;charset=UTF-8',
         'SOAPAction': '"http://clients.mindbodyonline./api/0_5/GetClasses"',
         'Content-Length': '594',
         'Connection': 'Keep-Alive',
         'User-Agent': 'Apache-HttpClient/4.1.1 (java 1.5)'
  }
}

var req = http.request(options, (res) => {
  var data;

  res.setEncoding('utf8');
  res.on('data', (chunk) => {
    data += chunk;
  });
  res.on('end', () => {
    parseString(data, function(err, result) {
      console.log(result);
    });
    console.log('End reached')
  });
});

req.on('error', (e) => {
  console.log(`problem with request: ${e.message}`);
});

req.write(SoapRequestXML);
req.end();

本文标签: javascriptReferenceError http is not definedStack Overflow