superagent 怎么请求到 301 跳转前的网页代码?
发布于 6 年前 作者 hanzichi 3580 次浏览 来自 问答

比如说 http://sf.gg 网站,直接用 superagent 请求到的是 301 跳转后的 https://segmentfault.com 的内容,我用 curl 命令获取 http://sf.gg 的内容如下:

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.4.6 (Ubuntu)</center>
</body>
</html>

我希望得到 301 前也就是如上的内容,如何用 superagent 实现? 看到 superagent 有个 Following redirects,这个需求和这个有关系吗?有的话应该怎么用?搞了很久没实现

望赐教,谢谢!

3 回复

手边没有环境,没法帮你测试

我猜。。 http://visionmedia.github.io/superagent/#response-properties

 request
   .get('http://sf.gg')
   .redirects(0)
   .then(function(res) {
   	  console.log(res.body)
	  console.log(res.headers['location'])
   })


@soda-wy 的确是使用.redirects(0), 不过对superagent 对301会抛错,要用catch去拿数据。

request
  .get('http://sf.gg')
  .redirects(0)
  .then(res => console.log(res))
  .catch(err => console.log(err.response.text)); // 目标内容

@samcsf

感谢🙏 的确如此

回到顶部