76

我正在尝试使用 nodejs请求模块将我重定向到另一个页面的 url 。

梳理文档,我找不到任何允许我在重定向后检索 url 的内容。

我的代码如下:

var request = require("request"),
    options = {
      uri: 'http://www.someredirect.com/somepage.asp',
      timeout: 2000,
      followAllRedirects: true
    };

request( options, function(error, response, body) {

    console.log( response );

});
4

5 回答 5

84

有两种非常简单的方法可以获取重定向链中的最后一个 url。

var r = request(url, function (e, response) {
  r.uri
  response.request.uri
})

uri 是一个对象。uri.href 包含带有查询参数的 url,作为字符串。

该代码来自请求创建者对 github 问题的评论:https ://github.com/mikeal/request/pull/220#issuecomment-5012579

例子:

var request = require('request');
var r = request.get('http://google.com?q=foo', function (err, res, body) {
  console.log(r.uri.href);
  console.log(res.request.uri.href);

  // Mikael doesn't mention getting the uri using 'this' so maybe it's best to avoid it
  // please add a comment if you know why this might be bad
  console.log(this.uri.href);
});

这将打印http://www.google.com/?q=foo三次(请注意,我们被重定向到一个没有 www 的地址)。

于 2013-06-28T10:48:16.487 回答
34

要查找重定向 url,请尝试以下操作:

var url = 'http://www.google.com';
request({ url: url, followRedirect: false }, function (err, res, body) {
  console.log(res.headers.location);
});
于 2013-12-22T22:58:55.297 回答
6

request默认获得重定向,默认可以通过10个重定向。您可以在docs中检查这一点。这样做的缺点是,您将不知道您获得的网址是重定向的网址还是默认选项的原始网址。

例如:

request('http://www.google.com', function (error, response, body) {
    console.log(response.headers) 
    console.log(body) // Print the google web page.
})

给出输出

> { date: 'Wed, 22 May 2013 15:11:58 GMT',
  expires: '-1',
  'cache-control': 'private, max-age=0',
  'content-type': 'text/html; charset=ISO-8859-1',
  server: 'gws',
  'x-xss-protection': '1; mode=block',
  'x-frame-options': 'SAMEORIGIN',
  'transfer-encoding': 'chunked' }

但是如果您将选项followRedirect设为 false

request({url:'http://www.google.com',followRedirect :false}, function (error, response, body) {
    console.log(response.headers) 
    console.log(body)
});

它给

> { location: 'http://www.google.co.in/',
  'cache-control': 'private',
  'content-type': 'text/html; charset=UTF-8',
  date: 'Wed, 22 May 2013 15:12:27 GMT',
  server: 'gws',
  'content-length': '221',
  'x-xss-protection': '1; mode=block',
  'x-frame-options': 'SAMEORIGIN' }
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.co.in/">here</A>.
</BODY></HTML>

所以不要担心得到重定向的内容。但是,如果您想知道它是否被重定向或未设置为followRedirectfalse 并检查location响应中的标头。

于 2013-05-22T15:19:52.827 回答
0

您可以使用followRedirect(而不是followAllRedirects)的函数形式,如下所示:

options.followRedirect = function(response) {
  var url = require('url');
  var from = response.request.href;
  var to = url.resolve(response.headers.location, response.request.href);
  return true;
};

request(options, function(error, response, body) {
  // normal code
});
于 2015-09-24T15:13:22.977 回答
0

如果你使用 axios,你可以像下面这样获取重定向的 url,

 axios({
        method: 'GET',
        url: URL,
        params: {
        }
    })
        .then((response) => {
            return response.request._redirectable._options.href;
        })
        .catch(error => {
            return error;
        });
于 2021-07-20T14:10:33.133 回答