我想知道如何将 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/