替换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); } }) }
问题分析与修复要点
- Axios调用参数顺序错误:axios.get的第一个参数必须是URL,第二个才是配置对象,你把配置对象当成URL传入,导致请求地址错误超时。
- 代理配置格式错误:tunnel的proxy.host不能带
http://前缀,只需填主机名proxy.crawlera.com;API密钥要单独放在auth字段,不能直接拼在host里。 - 缺失原Request的关键配置:原代码的
jar: true对应Axios的withCredentials: true,followAllRedirects: false对应Axios的maxRedirects: 0,这两项缺失会导致请求行为不一致。 - 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
相关产品推荐
相关产品推荐

