title: How to consume a REST api that needs username/password authentication in node.js
body: <p>I want to consume a REST api that needs a username/password authentication in node.js. The code that consumes the api is as follows:</p>
  
  <pre class="lang-js prettyprint-override"><code>var request = require('request');
  
  var url = 'http://localhost:3000/api/v1/login/'
  
  request.get(url,{'auth': {'user': 'test35','pass': 'mypassword','sendImmediately': false}},function(err, httpResponse, body) {
    if (err) {
      return console.error('post failed:', err);
    }
  
    console.log('Post successful!  Server responded with:', body);
  });
  </code></pre>
  
  <p>With the above code, the error I get is:</p>
  
  <pre class="lang-js prettyprint-override"><code>{
    "status": "error",
    "message": "API endpoint does not exist"
  }
  </code></pre>
  
  <p>The api is written in meteor restivus and you can see it in the following question's <a href="https://stackoverflow.com/questions/51287691/first-logging-in-is-not-required-when-using-meteor-restivus">answer here</a></p>
  
  <p>In the API, when I remove the api's authRequired: true, i.e, remove</p>
  
  <pre class="lang-js prettyprint-override"><code>{
          routeOptions: {
              authRequired: true
          }
    }
  </code></pre>
  
  <p>and in the code that consumes the API <strong>above</strong>, change url from</p>
  
  <blockquote>
    <p>'<a href="http://localhost:3000/api/v1/login/" rel="nofollow noreferrer">http://localhost:3000/api/v1/login/</a></p>
  </blockquote>
  
  <p>to:</p>
  
  <blockquote>
    <p><a href="http://localhost:3000/api/v1/articles/" rel="nofollow noreferrer">http://localhost:3000/api/v1/articles/</a></p>
  </blockquote>
  
  <p>and run "node accessRESTapi.js", I am able to consume the REST api! What I am not able to do correctly is the authentication when "authRequired: true" is set as per above! Please help</p>