javascriptaxiosresponse.redirect

Does Axios have the ability to detect redirects?


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.


Solution

  • Compare the request URL from your code with the URL given by response.request.responseURL; if those aren’t equal, you know the request was redirected.

    As far as the status code being 200, that’s what’s required by the spec; no intermediate status codes are exposed to frontend JavaScript running in a browser — regardless of which API you use. See also the answer at http://stackoverflow.com/a/46031800/441757