通过代理发送请求. request库工作,axios不工作

ipakzgxi  于 2023-01-25  发布在  iOS
关注(0)|答案(1)|浏览(149)

bounty将在2天后过期。回答此问题可获得+250声望奖励。Rilcon42希望引起更多人关注此问题。

我尝试更新一些旧代码来删除请求包,因为它不再被维护。我尝试用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);
        }
    })
}

工程

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);
      }
  })
}
7hiiyaii

7hiiyaii1#

请看一下axios docs。get-requests的方法签名是axios.get(url[, config]),但您的第一个参数实际上是一个对象。您可能需要使用axios({})并使用缺少的method键更新您的getOptions

let getOptions = {
  url: url,
  method: 'get', // this was missing!
  httpsAgent: tunnel.httpsOverHttp({
    ca: ca,
    proxy: {
      host: 'MY_API_KEY:@proxy.crawlera.com', // http is not needed, but it was http but you use httpsAgent?!
      port: 8011,
    },
  }),
  proxy: false,
} as any;

相关问题