通过 NPM 请求设置 Cookie 不起作用

Setting Cookies VIA NPM requests not working

// Require the request     
const request = require('request');

let api_url = 'https://some-api-that-requires-cookies.com'

// Setup the cookie jar to carry cookies
const cookieJar = request.jar();
let cookie = request.cookie('data=3');
cookieJar.setCookie(cookie);
// Add your cookie to the jar (URL is parsed into cookie parts)


// Send your request and include the cookie jar
request({url: api_url, jar: cookieJar},
    (error, response, body)=>{
        // do things
        console.log(response.headers['set-cookie'])
    }
);

输出:

[
  'sess_id=D8w23-9%!3832ed; Path=/CookieExample; Secure; HttpOnly'
]

我的 data=3 cookie 没有显示,我的响应仍然是访问被拒绝,(JSON 响应如果 data = 3 cookie 不存在)我的 cookie 没有注册吗?还是控制台日志只是在添加 cookie 之前执行?任何见解都是有帮助的

request 已完全弃用,例如使用 axios

所有语法都完整记录在官方 npm 库中。

然后:

const axios = require('axios')


axios.get(URL, {
            withCredentials: true, // to able getting cookies from the server back
            headers: {
                // put cookies in here
                Cookie: "cookie1=value; cookie2=value; cookie3=value;"
            }
        }).then(res =>{
          // res.data is your data back
        })