Reputation: 41
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)
Upvotes: 2
Views: 15356
Reputation: 24565
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.
Upvotes: 0
Reputation: 1
[non-Nodejs solution] maxRedirects is only supported in Nodejs (link). This means that if you use it in your browser (js code), redirects will still happen.
Alternative solution:
use fetch() with redirect
options is manual
(learn more here)
Example:
fetch(requestUrl, {
redirect: "manual",
})
.then((response) => {
// do something...
console.log(response);
// please note the headers will not be printable with console.log and the is no way to obtain the `Location` header but the other is ok
})
.catch((err) => {
console.error(err);
});
Upvotes: 0