request.js - 创建cookie

时间:2014-02-05 10:10:10

标签: node.js cookies request

我正在尝试向request.js模块(https://github.com/mikeal/request)添加一个cookie,但我迷路了

有没有人有这方面的经验?

我的代码是:

 var j = request.jar();
        var cookie = request.cookie('TENANTID');
        j.setCookie(cookie, 'public');


var requestSettings = {
            method: self.method[m],
            url: url.format({
                    pathname : self.getServer() + u,
                    query: {tenant: (process.env.STORM_DB_TENANTS_NAME || 'public')}
                }),
            headers: {'Content-Type': self.headers[h]},
            jar : true
        };

问题是如何将它们组合起来?

1 个答案:

答案 0 :(得分:2)

在您requestSettings中,您需要指定jar: j而不是jar: true

所以你的requestSettings看起来像这样:

var requestSettings = {
    method: self.method[m],
        url: url.format({
            pathname : self.getServer() + u,
            query: {tenant: (process.env.STORM_DB_TENANTS_NAME || 'public')}
        }),
    headers: {'Content-Type': self.headers[h]},
    jar : j    // The request.jar() object you created and added the cookie to.
};