I'm trying to follow through on a url that redirects me to another page using the nodejs request module.
我正在尝试跟踪一个url,该url使用nodejs请求模块将我重定向到另一个页面。
Combing through the docs I could not find anything that allows me to retrieve the url after the redirect.
在文档中搜索,我找不到任何允许我在重定向后检索url的东西。
My code is as follows:
我的代码如下:
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 个解决方案
#1
49
There are two very easy ways to get hold of the last url in a chain of redirects.
有两种非常简单的方法可以在重定向链中获取最后一个url。
var r = request(url, function (e, response) {
r.uri
response.request.uri
})
The uri is a object. uri.href contains the url, with query parameters, as a string.
uri是一个对象。uri。href包含具有查询参数的url作为字符串。
The code comes from a comment on a github issue by request's creator: https://github.com/mikeal/request/pull/220#issuecomment-5012579
该代码来自请求创建者对github问题的评论:https://github.com/mikeal/request/pull/220#issue -5012579
Example:
例子:
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);
});
This will print http://www.google.com/?q=foo three times (note that we were redirected to an address with www from one without).
这将打印http://www.google.com/?q=foo 3次(注意,我们被重定向到一个没有www的地址)。
#2
23
To find the redirect url try this:
要查找重定向url,请尝试以下操作:
var url = 'http://www.google.com';
request({ url: url, followRedirect: false }, function (err, res, body) {
console.log(res.headers.location);
});
#3
5
request
gets redirects by default, it can get through 10 redirects by default. You can check this in the docs. Downside of this is that you would not know if url you get is a redirected one or original one by default options.
请求被默认重定向,它可以通过10重定向被默认。你可以在文档中检查。缺点是你不知道你得到的url是重定向的还是默认的原始的。
For example:
例如:
request('http://www.google.com', function (error, response, body) {
console.log(response.headers)
console.log(body) // Print the google web page.
})
gives output
给出了输出
> { 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' }
but if you give option followRedirect
as false
但是如果你给了选项后面的选项是错误的。
request({url:'http://www.google.com',followRedirect :false}, function (error, response, body) {
console.log(response.headers)
console.log(body)
});
it gives
它给
> { 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>
So don't worry about getting the redirected content. But if you want to know if it is redirected or not set followRedirect
false and check the location
header in the response.
所以不要担心得到重定向的内容。但是如果您想知道它是否被重定向或没有设置followRedirect false并检查响应中的位置标头。
#4
0
You can use the function form for followRedirects
, like this:
您可以使用以下函数形式进行后续重定向:
options.followRedirects = 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
});
#1
49
There are two very easy ways to get hold of the last url in a chain of redirects.
有两种非常简单的方法可以在重定向链中获取最后一个url。
var r = request(url, function (e, response) {
r.uri
response.request.uri
})
The uri is a object. uri.href contains the url, with query parameters, as a string.
uri是一个对象。uri。href包含具有查询参数的url作为字符串。
The code comes from a comment on a github issue by request's creator: https://github.com/mikeal/request/pull/220#issuecomment-5012579
该代码来自请求创建者对github问题的评论:https://github.com/mikeal/request/pull/220#issue -5012579
Example:
例子:
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);
});
This will print http://www.google.com/?q=foo three times (note that we were redirected to an address with www from one without).
这将打印http://www.google.com/?q=foo 3次(注意,我们被重定向到一个没有www的地址)。
#2
23
To find the redirect url try this:
要查找重定向url,请尝试以下操作:
var url = 'http://www.google.com';
request({ url: url, followRedirect: false }, function (err, res, body) {
console.log(res.headers.location);
});
#3
5
request
gets redirects by default, it can get through 10 redirects by default. You can check this in the docs. Downside of this is that you would not know if url you get is a redirected one or original one by default options.
请求被默认重定向,它可以通过10重定向被默认。你可以在文档中检查。缺点是你不知道你得到的url是重定向的还是默认的原始的。
For example:
例如:
request('http://www.google.com', function (error, response, body) {
console.log(response.headers)
console.log(body) // Print the google web page.
})
gives output
给出了输出
> { 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' }
but if you give option followRedirect
as false
但是如果你给了选项后面的选项是错误的。
request({url:'http://www.google.com',followRedirect :false}, function (error, response, body) {
console.log(response.headers)
console.log(body)
});
it gives
它给
> { 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>
So don't worry about getting the redirected content. But if you want to know if it is redirected or not set followRedirect
false and check the location
header in the response.
所以不要担心得到重定向的内容。但是如果您想知道它是否被重定向或没有设置followRedirect false并检查响应中的位置标头。
#4
0
You can use the function form for followRedirects
, like this:
您可以使用以下函数形式进行后续重定向:
options.followRedirects = 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
});