Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS and HTTP Client - Are cookies supported?

NodeJS is a fantastic tool and blazing fast.

I'm wondering if HTTPClient supports cookies and if can be used in order to simulate very basic browser behaviour!


Help would be very much appreciated! =)


EDIT:

Found this: node-httpclient (seems useful!) not working!

like image 965
RadiantHex Avatar asked Apr 11 '10 01:04

RadiantHex


6 Answers

Short answer: no. And it's not so great.

I implemented this as part of npm so that I could download tarballs from github. Here's the code that does that: https://github.com/isaacs/npm/blob/master/lib/utils/fetch.js#L96-100

var cookie = get(response.headers, "Set-Cookie")
if (cookie) {
  cookie = (cookie + "").split(";").shift()
  set(opts.headers, "Cookie", cookie)
}

The file's got a lot of npm-specific stuff (log, set, etc.) but it should show you the general idea. Basically, I'm collecting the cookies so that I can send them back on the next request when I get redirected.

I've talked with Mikeal Rogers about adding this kind of functionality to his "request" util, complete with supporting a filesystem-backed cookiejar, but it's really pretty tricky. You have to keep track of which domains to send the cookies to, and so on.

This will likely never be included in node directly, for that reason. But watch for developments in userspace.

EDIT: This is now supported by default in Request.

like image 123
isaacs Avatar answered Oct 11 '22 17:10

isaacs


If you are looking to do cookies client side you can use https://github.com/mikeal/request

M.

like image 24
Martin Cleaver Avatar answered Oct 11 '22 19:10

Martin Cleaver


A feature-complete solution for cookies

The self-made solutions proposed in the other answers here don't cover a lot of special cases, can easily break and lack a lot of standard features, such as persistence.

As mentioned by isaacs, the request module now has true cookie support. They provide examples with cookies on their Github page. The examples explain how to enable cookie support by adding a "tough-cookie" cookie jar to your request.

NOTE: A cookie jar contains and helps you manage your cookies.

To quote their Readme (as of April 2015):

Cookies are disabled by default (else, they would be used in subsequent requests). To enable cookies, set jar to true (either in defaults or options) and install tough-cookie.

The cookie management is provided through the tough-cookie module. It is a stable, rather feature-complete cookie management tool that implements the "HTTP State Management Mechanism" - RFC 6265. It even offers a variety of options to persist (store) cookies, using a cookie store.

like image 42
Domi Avatar answered Oct 11 '22 19:10

Domi


Zombie.js is another choice if you want browser-like behaviour. It "maintains state across requests: history, cookies, HTML5 local and session stroage, etc.". More info on zombie's cookie api: http://zombie.labnotes.org/API

There is also PhantomJS and PhantomJS-based frameworks, like CasperJS.

like image 32
dgo.a Avatar answered Oct 11 '22 19:10

dgo.a


The code below demonstrates using cookie from server side, here's a demo API server that parse cookies from a http client and check the cookie hash:

var express = require("express"),
app     = express(),
hbs = require('hbs'),
mongoose = require('mongoose'),
port    = parseInt(process.env.PORT, 10) || 4568;

app.configure(function () {
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/public_api')); 
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.get('/api', function (req, res) {
    var cookies = {};
    req.headers.cookie && req.headers.cookie.split(';').forEach(function( cookie ) {
        var parts = cookie.split('=');
        cookies[ parts[ 0 ].trim() ] = ( parts[ 1 ] || '' ).trim();
    });
    if (!cookies['testcookie']) {
        console.log('First request');
        res.cookie('testcookie','testvaluecookie',{ maxAge: 900000, httpOnly: true });
        res.end('FirstRequest');
   } else {
        console.log(cookies['testcookie']);
        res.end(cookies['testcookie']);
   }
}); 

app.listen(port);

On the client side, just make a normal request to the server api above, i'm using request module, it by default transfers cookie for each request.

request(options, function(err, response, body) {    
    console.log(util.inspect(response.headers));
    res.render("index.html", {layout: false,user: {
        username: req.session.user + body
    }});
});
like image 37
revskill Avatar answered Oct 11 '22 17:10

revskill


Just get cookies from Set-Cookie param in response headers and send them back with future requests. Should not be hard.

like image 33
Kuroki Kaze Avatar answered Oct 11 '22 19:10

Kuroki Kaze