Does Axios have the ability to detect redirects?

匆匆过客 提交于 2020-05-15 02:52:41

问题


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.


回答1:


If axios exposes the equivalent of the fetch API’s response.url, you can compare the request URL from your code with the response URL; 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




回答2:


You can get the redirect count:

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


来源:https://stackoverflow.com/questions/55926127/does-axios-have-the-ability-to-detect-redirects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!