title: Node - Simple xml API Request
body: <p>I'm using the latest node.js (and express) to make an API call to a site that returns... XML... Ugh >_>. </p>
  
  <p>I've scowered the web and found a million different ways, but I don't know the latest, most up to date / best way to make a request and get a response in node/express. </p>
  
  <p>I tried using <a href="https://github.com/request/request" rel="nofollow noreferrer">https://github.com/request/request</a> and did the following: </p>
  
  <pre><code>var sendJsonResponse = function(res, status, content) {
      res.status(status);
      res.json(content);
  };
  
  var token = request
      .get('some-website.com/api/stuff')
      .on('response', function(response) {
          console.log(response.statusCode);
          console.log(response.headers['content-type']);
      });
  
  sendJsonResponse(res, 200, token);
  </code></pre>
  
  <p>in the <code>console.log</code> statements I get <code>200</code> and then <code>application/xml;charset=utf-8</code>. </p>
  
  <p>But on my page I don't get the xml I'm looking for. Any ideas? I've tried using <a href="https://github.com/Leonidas-from-XIV/node-xml2js" rel="nofollow noreferrer">https://github.com/Leonidas-from-XIV/node-xml2js</a> to attempt to "parse" the response, in case node just can't handle the xml response, but to no avail. </p>
  
  <pre><code>var xml2js = require('xml2js');
  parser.parseString(response, function(err, result) {
      console.dir(result);
      console.log('Done');
  });
  </code></pre>
  
  <p>Any help on accessing an API using Node and actually using the XML response, please?</p>
  
  <p><strong><em>EDIT ANSWER</em></strong></p>
  
  <p>For the Node.js request and xml parsing of the returned xml content: </p>
  
  <pre><code>var request = require('request');
  var xml2js = require('xml2js');
  
  var sendJsonResponse = function(res, status, content) {
      res.status(status);
      res.json(content);
  };
  
  /* GET XML Content*/
  module.exports.dsRequest = function(req, res) {
  
      var parser = new xml2js.Parser();
  
      request('url_for_xml_request', function(error, response, body) {
          parser.parseString(body, function(err, result) {
              sendJsonResponse(res, 200, result);
          });
  
      });
  
  };
  </code></pre>