Does Axios have the ability to detect redirects?

Daniel Bank picture Daniel Bank · Apr 30, 2019 · Viewed 25.2k times · Source

The Response interface of the Fetch API has a read-only flag, redirected, which indicates whether or not the response was the result of a request that was redirected.

Does the axios library have a similar capability? The best I could find is maxRedirects which sets the maximum number of redirects to follow. However, I am simply looking to determine if it happened or not (so I can handle redirects specially), not prevent it from happening.

Answer

Populus picture Populus · Oct 17, 2019

You can get the redirect count:

const response = await axios.get('http://someurlthatredirects.com');
console.log(response.request._redirectable._redirectCount);