I'm currently using request-promise in nodejs to make a request to a website and then return the headers, as I'm trying to get the url (location) of the request incase of redirects. Although the issue I'm having is that the location is not showing up in the headers when I'm logging them after the request.
My code:
const rp = require('request-promise');
const userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36';
const url = //url
var _include_headers = function(body, response, resolveWithFullResponse) {
return {'headers': response.headers, 'data': body};
};
const options = {
uri: url,
followAllRedirects: true,
method: 'get',
gzip: true,
transform: _include_headers,
headers: {
'User-Agent': userAgent
},
};
const p1 = rp(options).then((response, error, html) => {
console.log(response.headers);
})
The console logs the headers, but it doesn't show the location in the headers object. Is there anyway I can find the location of the url after making the request and returning the headers?
When you set
followAllRedirects
totrue
, you won't have alocation
field in your response headers, and the only way to access the final redirected URL is to access theresponse.request.uri
object that contains the final URL in itshref
property.To do so, you can modify your
transform
function to pass an additionalfinalUrl
property that is equal toresponse.request.uri.href
:In the example above,
http://www.stackoverflow.com
is redirected tohttps://stackoverflow.com
, which is what the program prints.