Node.JS: Simple GET with axios module throwing “Error: Max redirects exceeded”

China☆狼群 提交于 2020-02-06 07:56:08

问题


Was trying to understand the basics of Web Scraping & was successful doing it with Python. When trying to emulate the same with Node getting following error :

UnhandledPromiseRejectionWarning: Error: Max redirects exceeded.

Below is simple code snippet that am trying to run using node scrape.js :

const axios = require('axios');

const url = 'https://www.somewebsite.com/auth/get_menu/?city_id=1';
const headers = {
    'accept': '*/*',
    'content-type': 'application/json',
    'app_client': 'consumer_web'
};

axios.get(url, {
    headers,
    maxRedirects: 0
  })
.then(resp => {
    console.log(resp.body);
})
.catch(error => {
    if (error.response) {
      console.log(error.response.data);
      console.log('---------------------------------------------');
      console.log(error.response.status);
      console.log('---------------------------------------------');
      console.log(error.response.headers);
    } else if (error.request) {
      console.log(error.request);
    } else {
      // Something happened in setting up the request that triggered an Error
      console.log('Error', error.message);
    }
    console.log(error.config);
});

NOTE : As pointed out in comments - the URL with those headers, does a 302 redirect to /skip_explore/?c=1&n=/auth/get_menu/?city_id=1 which then does a redirect to /auth/get_menu/?city_id=1 which is now a loop that lasts forever.

Please let me where am doing it wrong. Thanks in advance.

来源:https://stackoverflow.com/questions/59851293/node-js-simple-get-with-axios-module-throwing-error-max-redirects-exceeded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!