55

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?

3 Answers 3

55

This answer is deprecated, please see @ankitjaininfo's answer below for a more modern solution


Here's how I think you make a POST request with data and a cookie using just the node http library. This example is posting JSON, set your content-type and content-length accordingly if you post different data.

// NB:- node's http client API has changed since this was written
// this code is for 0.4.x
// for 0.6.5+ see http://nodejs.org/docs/v0.6.5/api/http.html#http.request

var http = require('http');

var data = JSON.stringify({ 'important': 'data' });
var cookie = 'something=anything'

var client = http.createClient(80, 'www.example.com');

var headers = {
    'Host': 'www.example.com',
    'Cookie': cookie,
    'Content-Type': 'application/json',
    'Content-Length': Buffer.byteLength(data,'utf8')
};

var request = client.request('POST', '/', headers);

// listening to the response is optional, I suppose
request.on('response', function(response) {
  response.on('data', function(chunk) {
    // do what you do
  });
  response.on('end', function() {
    // do what you do
  });
});
// you'd also want to listen for errors in production

request.write(data);

request.end();

What you send in the Cookie value should really depend on what you received from the server. Wikipedia's write-up of this stuff is pretty good: http://en.wikipedia.org/wiki/HTTP_cookie#Cookie_attributes

9
  • I'll be sending JSON data itself, so this is fine, but for uniformity with the other tests, I was hoping there was a way to do this using the restler library
    – Vanwaril
    Jan 3, 2011 at 3:32
  • 1
    I see, it wasn't clear from your question that restler was important, sorry. The README at github.com/danwrong/restler says that the post method can specify headers in the options argument. You just need to put the Cookie there instead: rest.post(url, { data: 'your data', headers { Cookie: 'your cookie' } })
    – RandomEtc
    Jan 3, 2011 at 4:34
  • Oh, I tried lowercase 'cookie'. Anyway, I already rewrote it to use http client, so I'll think about converting it back later.
    – Vanwaril
    Jan 3, 2011 at 16:56
  • @Vanwaril: I wouldn't bother writing it back, I have an eerie feeling that it is not case sensitive and you would be wasting time, because the only reason it would be is if there was some strange code in restler that is against the typical for HTTP. May 3, 2011 at 15:21
  • 2
    The use of http.createClient is now deprecated. Refer my new answer. Apr 14, 2014 at 9:15
48

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();
1
  • Thanks a million @ankitjaininfo. I was stuck trying to follow what says the HTTP Node documentation : "set-cookie and cookie headers which are represented as an array of values" link
    – Bludwarf
    Oct 7, 2015 at 17:16
1

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'   
    }
});
1
  • Thank you so much for this!
    – Samuel
    Oct 16, 2018 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.