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.
redirect() The redirect() method of the Response interface returns a Response resulting in a redirect to the specified URL. Note: This is mainly relevant to the ServiceWorker API. A controlling service worker could intercept a page's request and redirect it as desired.
To follow redirect with Curl, use the -L or --location command-line option. This flag tells Curl to resend the request to the new address. When you send a POST request, and the server responds with one of the codes 301, 302, or 303, Curl will make the subsequent request using the GET method.
Yes, XMLHttpRequest object (according to the standard) must automatically handle redirection without giving the client code a chance to do anything about it.
Axios is a promise-based HTTP client library that makes it simple to send asynchronous HTTP requests (such as POST , GET , and DELETE ) to REST endpoints, mainly APIs. In this article, we will learn how to send POST JSON requests with Axios, and how to handle both previously serialized and unserialized data.
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
You can get the redirect count:
const response = await axios.get('http://someurlthatredirects.com');
console.log(response.request._redirectable._redirectCount);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With