Search code examples
node.jsaxios

Axios 302 responses


I'm trying to get the response headers of a website that is "temporarily moved" whenever I do so it just redirects me. I've done it with fiddler to try and get a clear image in my head and I just can't wrap my head around how I would do it with Axios. Basically I'm trying to get the headers of the site before the redirect happens. If that isn't possible what else should I use to do so automatically?

const axios = require('axios')
const readline = require('readline');
const title = require('node-bash-title')
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});
console.clear()
var loopline = function () {
rl.question('Input: ', (answer) => {    
    axios.get(`https://example302site.com/${answer}`, {
      .then((response) => {
        console.log(response.headers)
      }, (error) => {
        console.log(`AssetId: ${error.response.body}`)
        loopline()
    })
})
}
loopline()

(Sorry for messy code)


Solution

  • Axios has a request config option maxRedirects which allows you to control the number of redirects. Setting it to 0 should prevent getting redirected and allows you to read the response-headers:

    axios.get(`https://assetgame.roblox.com/asset/?versionid=5667870400`, {
            headers: { 'User-Agent': 'Roblox/WinInet' },
            maxRedirects: 0
          })
    

    Note: works only in nodejs, not in the browser. See Dao's answer.