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?

Vanwaril
  • 7,380
  • 6
  • 33
  • 47

3 Answers3

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

RandomEtc
  • 1,976
  • 1
  • 18
  • 17
  • 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 03 '11 at 03:32
  • 1
    I see, it wasn't clear from your question that restler was important, sorry. The README at https://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 03 '11 at 04: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 03 '11 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. – 700 Software May 03 '11 at 15:21
  • @RandomEtc is "data.length" really correct for unicode (multi-byte) strings? – Marc Sep 08 '11 at 18:45
  • @Marc good catch, thanks! Updated to use node's Buffer.byteLength(data,'utf8') instead, which I think is correct. – RandomEtc Dec 07 '11 at 23:29
  • 2
    The use of `http.createClient` is now deprecated. Refer my new answer. – ankitjaininfo Apr 14 '14 at 09:15
  • Just to confirm - `headers { Cookie: 'your cookie' }` in restler - works perfect for me. – Roman Pietrzak Oct 25 '14 at 12:19
  • How about https request? I tried replacing http with https and it didn't seem to work... – James Wayne Sep 28 '16 at 21:03
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();
ankitjaininfo
  • 11,961
  • 7
  • 52
  • 75
  • 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](https://nodejs.org/api/http.html#http_http) – Bludwarf Oct 07 '15 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'   
    }
});
ranm8
  • 1,085
  • 10
  • 10