node.js - 如何在请求中使用 cookie(request、tough-cookie、node.js)

标签 node.js cookies request

我想知道如何将 cookie 与 request 一起使用( https://github.com/mikeal/request )

我需要设置一个能够从请求中为每个子域获取的 cookie,

有点像

*.examples.com

路径是针对每个页面的,比如

/

然后服务器端能够正确地从 cookie 中获取数据,比如

test=1234

我发现根据响应设置的 cookie 工作正常,

我添加了一个自定义 jar 来保存 cookie,比如

var theJar = request.jar();

var theRequest = request.defaults({
            headers: { 
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36' 
            } 
          , jar: theJar
        });

但我根据请求设置的 cookie 只能在同一域中获取,

我在更多选项中找不到设置cookie的方法

现在,如果我想要一个能够在三个子域中获取的 cookie,

我必须这样设置:

theJar.setCookie('test=1234', 'http://www.examples.com/', {"ignoreError":true});

theJar.setCookie('test=1234', 'http://member.examples.com/', {"ignoreError":true});

theJar.setCookie('test=1234', 'http://api.examples.com/', {"ignoreError":true});

Is here any advance ways to setup a cookie from request,

made it able to be fetched in every sub domains ???

最佳答案

我刚刚找到解决方案....

theJar.setCookie('test=1234; path=/; domain=examples.com', 'http://examples.com/');

嗯...我不得不说,请求的文件不是很好...,哈哈

关于node.js - 如何在请求中使用 cookie(request、tough-cookie、node.js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23336552/

相关文章:

javascript - 如何在 HTML 页面中嵌入 PDF?

mysql - Node js 使用连接池和多个参数调用mysql中的存储过程

ios - 在 iOS5 中让 cookie 与 PhoneGap 一起使用吗?

带有自定义 header 的 Python grequests

node.js - 如何将不同请求的响应与nodejs和请求放在同一个文档中?

javascript - 带有 gzip 的 XMLHttpRequest

node.js - npm 的 Vagrant 文件系统问题

windows - 在 Node JS 上生成 (Windows Server 2012)

javascript - 在 Rails 中创建的 JSON 字符串用加号 (+) 替换空格

python - 如何使用 urlfetch 获取所有 cookie?