[http-headers] How do I create a HTTP Client Request with a cookie?

I've got a node.js Connect server that checks the request's cookies. To test it within node, I need a way to write a client request and attach a cookie to it. I understand that HTTP Requests have the 'cookie' header for this, but I'm not sure how to set it and send -- I also need to send POST data in the same request, so I'm currently using danwrong's restler module, but it doesn't seem to let me add that header.

Any suggestions on how I can make a request to the server with both a hard-coded cookie and POST data?

This question is related to http-headers node.js

The answer is


You can do that using Requestify, a very simple and cool HTTP client I wrote for nodeJS, it support easy use of cookies and it also supports caching.

To perform a request with a cookie attached just do the following:

var requestify = require('requestify');
requestify.post('http://google.com', {}, {
    cookies: {
        sessionCookie: 'session-cookie-data'   
    }
});

The use of http.createClient is now deprecated. You can pass Headers in options collection as below.

var options = { 
    hostname: 'example.com',
    path: '/somePath.php',
    method: 'GET',
    headers: {'Cookie': 'myCookie=myvalue'}
};
var results = ''; 
var req = http.request(options, function(res) {
    res.on('data', function (chunk) {
        results = results + chunk;
        //TODO
    }); 
    res.on('end', function () {
        //TODO
    }); 
});

req.on('error', function(e) {
        //TODO
});

req.end();