如何从nodejs请求模块获取重定向的URL?

hit*_*uct 64 module request node.js

我正在尝试通过使用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 );

});
Run Code Online (Sandbox Code Playgroud)

gab*_*elf 66

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

var r = request(url, function (e, response) {
  r.uri
  response.request.uri
})
Run Code Online (Sandbox Code Playgroud)

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);
});
Run Code Online (Sandbox Code Playgroud)

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

  • 如果给定的 url 是像 'sdfdsfdgdfgdfgdfg.sdfsdfsdf' 这样的坏 url,`res.request.uri.href` 会崩溃,所以如果你想使用这个选项,你可以检查 `err` 或是否存在 `res`。 (4认同)

Mic*_*ael 30

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

var url = 'http://www.google.com';
request({ url: url, followRedirect: false }, function (err, res, body) {
  console.log(res.headers.location);
});
Run Code Online (Sandbox Code Playgroud)

  • `res.headers.location`为我做了这份工作. (7认同)
  • 如果涉及多个重定向,则可能无法按预期工作,从而提供第一个重定向而不是最后一个重定向. (4认同)

use*_*109 5

request默认情况下获取重定向,默认情况下可以通过10次重定向.您可以在文档中查看此内容.这样做的缺点是你不知道你得到的网址是默认选项的重定向或原始网址.

例如:

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

给出输出

> { 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' }
Run Code Online (Sandbox Code Playgroud)

但是如果你把选项followRedirect作为假

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

它给

> { 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>
Run Code Online (Sandbox Code Playgroud)

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