NodeJS axios-cookiejar-support不支持与其他http(s)一起使用,Agent

sd2nnvve  于 2023-01-16  发布在  Node.js
关注(0)|答案(1)|浏览(250)

我的密码是

const axios = require('axios')
const https = require('https');

const axiosCookieJarSupport = require("axios-cookiejar-support");
const tough = require("tough-cookie");

const HEADERS = {
    'Content-Type': 'application/json; odata.streaming=true; charset=utf-8',
    Accept: 'application/json; odata.metadata=none, text/plain',
    'TM1-SessionContext': '----',
    'User-Agent': '----'
  }
  
this.http = axios.create({
   headers: HEADERS,
   withCredentials: true,
   httpsAgent: new https.Agent({ rejectUnauthorized: false })
})
        
axiosCookieJarSupport.wrapper(this.http)
this.http.defaults.jar = new tough.CookieJar()
this.http.defaults.baseURL = this.baseUrl
    
// Then other things but that doesn't matter

但是当我运行时,我得到了这个错误:

Error: axios-cookiejar-support does not support for use with other http(s).Agent.
    at requestInterceptor (C:\node_modules\axios-cookiejar-support\dist\index.js:14:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async RestService.startSession (C:\main.js:109:29)

我猜问题出在用https. Agent({})初始化的https代理上
可能存在相同的构造函数从axios库,但我没有找到它
先谢谢你

qxgroojn

qxgroojn1#

https://github.com/3846masa/axios-cookiejar-support/issues/426https://github.com/3846masa/axios-cookiejar-support/issues/431可以回答类似的问题
简而言之,您希望使用自定义HTTPS代理httpsAgent: new https.Agent({ rejectUnauthorized: false }),但axios-cookiejar-support无法与之配合使用。
尝试使用http-cookie-agent代替axios-cookiejar-support,如下所示:

const { HttpsCookieAgent } = require('http-cookie-agent');

const jar = new tough.CookieJar();
const httpsAgent = new HttpsCookieAgent({
  jar,
  keepAlive: true,
  rejectUnauthorized: false,
});

this.http = axios.create({
   headers: HEADERS,
   withCredentials: true,
   httpsAgent,
});
        
this.http.defaults.jar = jar;
this.http.defaults.baseURL = this.baseUrl;

相关问题