【问题标题】:how to using cookie with request (request , tough-cookie , node.js)如何在请求中使用 cookie(request、tough-cookie、node.js)
【发布时间】:2014-04-28 17:16:37
【问题描述】:

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

我需要设置一个 cookie,它可以从请求中为每个子域获取,

类似

*.examples.com

路径适用于每个页面,例如

/

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

测试=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});

这里有任何从请求中设置 cookie 的高级方法吗,

使其能够在每个子域中获取???

【问题讨论】:

    标签: node.js cookies request


    【解决方案1】:

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

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

    hm...我不得不说,请求的文件不太好...,哈哈

    【讨论】:

    • 我正在向 localhost 发出 http 请求,但请求中没有携带 cookie。如何确保使用带有 jar 的请求在标头中携带 cookie:true?
    • setCookie 看起来真的很奇怪......它不会自动检测“安全”,它不会从我传递的 URL 设置域,并且使用“ignoreError”会导致回调永远不会被调用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 2020-12-26
    • 2021-10-30
    • 2019-09-07
    相关资源
    最近更新 更多