You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

替换request为axios后代理请求超时,请求排查配置问题

问题:Axios替换Request代理请求超时,原Request代码正常运行

由于request包已停止维护,我尝试将旧代码中的代理请求替换为axios实现,但出现超时问题无法正常运行,而原request代码可正常工作。请问是否遗漏了axios的相关配置?


失败的Axios代码

export function sendAxiosApiRequest(enableProxy, proxyIndex,url,filepath?:string):object {
  //https://support.zyte.com/support/discussions/topics/22000014602
  let ca='READ IN FILE HERE'
  
    let getOptions = {
        url: url,
        httpsAgent: tunnel.httpsOverHttp({
          ca: ca,
          proxy: {
            host: 'http://MY_API_KEY:@proxy.crawlera.com',
            port: '8011',
          },
        }),
        proxy: false, //disable auto config, bc we set it manually
    } as any;
    console.log({getOptions})

    return new Promise(resolve => {
        try {
            axios.get(getOptions,(err,response,html)=>{
                if(err){
                    console.log(err);
                    resolve(false);
                }
                else {
                  try{
                    const output = JSON.parse(html);
                    resolve(output);
                  }catch(e){
                    console.log({html})
                    throw `ERROR parsing html: `+JSON.stringify(e)
                  }
                }
            })
        }
        catch (e) {
            console.log(`Err parsing result from sendApiRequest:`,e);
            resolve(false);
        }
    })
}

成功的Request代码

export function sendRequestApiRequest(enableProxy, proxyIndex,url,filepath?:string):object {

  let ca='READ IN FILE HERE'
  let getOptions = {
      url: url,
      jar: true,
      followAllRedirects: false,
  } as any;
  //console.log({filepath})
      getOptions.proxy= 'http://MY_API_KEY:@proxy.crawlera.com'
      getOptions.ca=ca
          getOptions.requestCert =true
          getOptions.rejectUnauthorized= true

  return new Promise(resolve => {
      try {
          request.get(getOptions,(err,response,html)=>{
              if(err){
                  console.log(err);
                  resolve(false);
              }
              else {
                  const output = JSON.parse(html);
                  resolve(output);
              }
          })
      }
      catch (e) {
          console.log(e);
          resolve(false);
      }
  })
}

问题分析与修复要点

  1. Axios调用参数顺序错误:axios.get的第一个参数必须是URL,第二个才是配置对象,你把配置对象当成URL传入,导致请求地址错误超时。
  2. 代理配置格式错误:tunnel的proxy.host不能带http://前缀,只需填主机名proxy.crawlera.com;API密钥要单独放在auth字段,不能直接拼在host里。
  3. 缺失原Request的关键配置:原代码的jar: true对应Axios的withCredentials: true,followAllRedirects: false对应Axios的maxRedirects: 0,这两项缺失会导致请求行为不一致。
  4. Axios回调写法不符合规范:Axios本身基于Promise,不需要像Request那样传回调函数,错误的回调写法会导致异常处理失效。

修复后的Axios代码示例

export function sendAxiosApiRequest(enableProxy, proxyIndex, url, filepath?: string): Promise<object | false> {
  let ca = 'READ IN FILE HERE';

  const config = {
    httpsAgent: tunnel.httpsOverHttp({
      ca: ca,
      proxy: {
        host: 'proxy.crawlera.com',
        port: 8011,
        auth: {
          username: 'MY_API_KEY',
          password: ''
        }
      },
    }),
    proxy: false,
    withCredentials: true,
    maxRedirects: 0,
    responseType: 'text'
  };

  return axios.get(url, config)
    .then(response => {
      try {
        return JSON.parse(response.data);
      } catch (e) {
        console.log({ html: response.data });
        throw new Error(`ERROR parsing html: ${JSON.stringify(e)}`);
      }
    })
    .catch(err => {
      console.log(err);
      return false;
    });
}

内容的提问来源于stack exchange,提问作者Rilcon42

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 07:10:43