首页 > 解决方案 > nodejs request.post 无法获取cookies,但是request.get 是可以的。

问题描述

当我使用代码登录网站后。我想发送一个帖子请求,服务器无法获取 cookie,但获取请求是可以的。

我的代码:

var req = require('request');
req.defaults({ jar: true });

 const loginUrl = 'https://www.a.com/auth/login';
 const scoreUrl = 'https://app-test.a.com/bms/CustomerAccount/member/get-list-task?_ac=OperationCenter&_smp=OperationCenter.Member';

function login(options, cb) {

req.post({
    url: loginUrl,
    form: {
        'tenantCode': 'jfdc',
        'userName': 'jfdc',
        'password': 'sh123456'
    }
},
    function (err, res, body) {
        var cb = function (err, res, body) {
            console.log(body);
        };

        options = {
            url: scoreUrl,
            data: {
                page: 1,
                pageSize: 10,
                corp_id: '11b11db4-e907-4f1f-8835-b9daab6e1f23',
                key: '111111111111111',
                sort_list: [],
                _t: 1534647594968
            }
        };
        req.post(options, cb);
    });
}

login();

谢谢!!!

标签: node.jspostcookiesrequest

解决方案


You can find cookie in:

res.headers['set-cookie']

and use it for next request.


推荐阅读