title: Basic Authentication for HTTP request in AWS Lambda node.js
body: <p>I have a http request in my AWS Lambda function using node.js. The code works fine, however, I want to access a URL that needs a username/password authentication. How can I implement it in my code?</p>
  
  <pre><code>function httprequest() {
       return new Promise((resolve, reject) =&gt; {
          const options = {
              host: 'api.plos.org',
              path: '/search?q=title:DNA',
              port: 443, 
              method: 'GET'
          };
          const req = http.request(options, (res) =&gt; {
            if (res.statusCode &lt; 200 || res.statusCode &gt;= 300) {
                  return reject(new Error('statusCode=' + res.statusCode));
              }
              var body = [];
              res.on('data', function(chunk) {
                  body.push(chunk);
              });
              res.on('end', function() {
                  try {
                      body = JSON.parse(Buffer.concat(body).toString());
                  } catch(e) {
                      reject(e);
                  }
                  resolve(body);
              });
          });
          req.on('error', (e) =&gt; {
            reject(e.message);
          });
  
         req.end();
      });
  }
  </code></pre>