How to get the redirected URL from the Node.js request module?

Sometimes, we want to get the redirected URL from the Node.js request module.

In this article, we’ll look at how to get the redirected URL from the Node.js request module.

How to get the redirected URL from the Node.js request module?

To get the redirected URL from the Node.js request module, we can get the redirected URL from res.headers.location property.

For instance, we write

const url = 'http://www.google.com';

request({
  url,
  followRedirect: false
}, (err, res, body) => {
  console.log(res.headers.location);
});

to call request with an object that has the followRedirect property set to true to follow redirects.

Then we can get the redirected URL from res.headers.location in the request callback after the request is done.

Conclusion

To get the redirected URL from the Node.js request module, we can get the redirected URL from res.headers.location property.