【问题标题】:Cant seem to make cookie authenticated requests using Unirest似乎无法使用 Unirest 发出经过 cookie 身份验证的请求
【发布时间】:2015-10-12 10:49:34
【问题描述】:

我正在尝试使用Unirest Jar 功能来发出会话身份验证请求,但我似乎无法让我的名称控制台记录。以下是我使用的示例代码。关于如何让我的名称控制台记录的任何想法?

var Cheerio = 要求('cheerio'); var unirest = require('unirest'); var CookieJar = unirest.jar(); var twitterLogin = 'https://twitter.com/sessions'; var twitterUsername = 'TWITTERUSERNAME'; var twitterPassword = 'TWITTERPASSWORD!' var Request = unirest.get('https://twitter.com/login').jar(CookieJar); 请求结束(函数(响应){ var $=cheerio.load(response.body); var authToken = $('input[name="authenticity_token"]').val(); console.log(authToken) unirest.post(twitterLogin) .jar(饼干罐) .followRedirect(真) .header('Referer', 'https://twitter.com/login') .header('content-type', 'application/x-www-form-urlencoded') .header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36') .field('authenticity_token', authToken) .field('session[username_or_email]', twitterUsername) .field('session[密码]', twitterPassword) .end(函数(响应){ console.log(response.statusCode) 控制台.log(response.body) unirest.get('https://twitter.com') .jar(饼干罐) .followRedirect(真) .end(函数(响应){ var $=cheerio.load(response.body) console.log($('.DashboardProfileCard-name').text()); }) }) });

【问题讨论】:

    标签: node.js unirest


    【解决方案1】:

    来自the docs

    Request.jar(Boolean) 或 Request.jar(Jar)

    Request.options 上设置jar,cookie 容器。当设置为 true 时,它会存储 cookie 以供将来使用。

    您应该使用true 作为参数调用unirest.jar()

    var CookieJar = unirest.jar(true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-20
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      • 2012-05-08
      • 2021-12-31
      • 1970-01-01
      • 2019-03-29
      相关资源
      最近更新 更多