Pass cookie as part of node.js request
<p>I am using the <code>request</code> package to create my server side requests. I wrote authentication middleware that checks for a cookie/session id for all requests. Therefore, is there a way I include the user's cookie as part of the request? Here is my current code:</p>

<pre><code>var cookie = parseCookie.parseCookie(req.headers.cookie);

request('http://localhost:3000/users/api', function(error, response, body) {
    console.log(body); //this console.logs my login page since requests w/o valid cookies get redirected to login
    res.render('../views/admin');
});
</code></pre>

<p>Currently, this returns 'no cookie found' in the console. However, if I turn off my authentication middleware, the code above works as intended.</p>

<p>Additional info:</p>

<p>The cookie I want is the end user's cookie located on the browser. The end user's cookie is created by the app whenever the user logs in.</p>

<p>Update - solution attempt 1:</p>

<p>I tried this from the documentation:</p>

<pre><code>var cookie = parseCookie.parseCookie(req.headers.cookie);
var cookieText = 'sid='+cookie;
var j = request.jar();
        var cookie = request.cookie(cookieText);
        var url = 'http://localhost:3000/users/api';
        j.setCookie(cookie, url);
        request({url: url, jar: j}, function(error, response, body) {
            request('http://localhost:3000/users/api');
        });
</code></pre>

<p>However, the console is still returning 'no cookie found'</p>

<p>Can someone help?</p>

<p>Thanks in advance!</p>