NodeJS and HTTP Client - Are cookies supported?

2019-02-11 14:45发布

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!

6条回答
我欲成王,谁敢阻挡
2楼-- · 2019-02-11 15:10

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
    }});
});
查看更多
Rolldiameter
3楼-- · 2019-02-11 15:13

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

查看更多
男人必须洒脱
4楼-- · 2019-02-11 15:14

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.

查看更多
做个烂人
5楼-- · 2019-02-11 15:19

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

M.

查看更多
Deceive 欺骗
6楼-- · 2019-02-11 15:19

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.

查看更多
Lonely孤独者°
7楼-- · 2019-02-11 15:25

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.

查看更多
登录 后发表回答