node.js 请求获取重定向链
node.js request get redirect chain
是否可以使用 request
模块查看整个重定向链,例如 puppeteer
是如何做到的?
我希望能够看到每个状态代码/urls/当我访问一个站点时发生了多少次重定向
例如,如果我请求“http://apple.com”
url 设置为重定向到
https://www.apple.com
(本例中链为1)
我想知道 (1) 重定向发生了,以及 (2) 需要多少次重定向才能到达那个
如果 request
无法做到这一点,还有其他库吗? (我不再使用 puppeteer
因为 puppeteer
不能很好地处理测试附件)
想通了,是的,完全可以。
const request = require('request')
request.get({
uri: 'http://apple.com',
followAllRedirects: true
}, function (err, res, body) {
console.log(res.request._redirect.redirectsFollowed)
console.log(res.request._redirect.redirects) // this gives the full chain of redirects
});
不仅可以,而且更容易使用:
重定向对象:https://github.com/request/request/blob/master/lib/redirect.js
request.get (
{
uri: `http://somesite.com/somepage`,
followAllRedirects: true
},
(err, res, body) => {
if (err) {
// there's an error
}
if (!res) {
// there isn't a response
}
if (res) {
const status = res.statusCode; // 404 , 200, 301, etc
const chain = res.request._redirect.redirects; // each redirect has some info too, see the redirect link above
const contentType = res.headers["content-type"] // yep, you can do this too
}
}
)
是否可以使用 request
模块查看整个重定向链,例如 puppeteer
是如何做到的?
我希望能够看到每个状态代码/urls/当我访问一个站点时发生了多少次重定向
例如,如果我请求“http://apple.com” url 设置为重定向到
https://www.apple.com
(本例中链为1)
我想知道 (1) 重定向发生了,以及 (2) 需要多少次重定向才能到达那个
如果 request
无法做到这一点,还有其他库吗? (我不再使用 puppeteer
因为 puppeteer
不能很好地处理测试附件)
想通了,是的,完全可以。
const request = require('request')
request.get({
uri: 'http://apple.com',
followAllRedirects: true
}, function (err, res, body) {
console.log(res.request._redirect.redirectsFollowed)
console.log(res.request._redirect.redirects) // this gives the full chain of redirects
});
不仅可以,而且更容易使用:
重定向对象:https://github.com/request/request/blob/master/lib/redirect.js
request.get (
{
uri: `http://somesite.com/somepage`,
followAllRedirects: true
},
(err, res, body) => {
if (err) {
// there's an error
}
if (!res) {
// there isn't a response
}
if (res) {
const status = res.statusCode; // 404 , 200, 301, etc
const chain = res.request._redirect.redirects; // each redirect has some info too, see the redirect link above
const contentType = res.headers["content-type"] // yep, you can do this too
}
}
)