Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interceptor for 302 responses #980

Closed
paul-piro opened this issue Jun 27, 2017 · 9 comments
Closed

Interceptor for 302 responses #980

paul-piro opened this issue Jun 27, 2017 · 9 comments

Comments

@paul-piro
Copy link

Hi,
I have a problem with 302 responses, we need to access headers and cookies after the user authenticates the response comes with set-cookie and with a 302 status for redirection, we are using nodejs with custom cookie jar and we save cookies in interceptors but there is no interceptor for 302 responses.

Is there any solution to this?

@pimlie
Copy link

pimlie commented Jun 27, 2017

Set maxRedirects: 0 in Request Config?

@paul-piro
Copy link
Author

I did that but i consider it a hack because after i get the redirect i need to manually request the location in the header

@pimlie
Copy link

pimlie commented Jun 27, 2017

Unfortunately it seems there is no other way, actually axios handles no redirects at all. The maxRedirects option is passed to the http(s) transport from nodejs. The http client from nodejs does the automatic redirect (at least on server side)

@rubennorte
Copy link
Member

rubennorte commented Aug 12, 2017

You can use maxRedirects: 0 (and a custom validateStatus option to accept 3xx) and a response interceptor to handle additional requests.

@matepaiva
Copy link

matepaiva commented Feb 2, 2019

I arrived here because I had a similar problem: the server was redirecting when the user was not logged in and I was trying to find a way to intercept it. But it turns out that, in my case, when I added a common header to every request telling that it is a XHR, the server started responding correctly. Just writing it down to maybe help another person with the same issue.

axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

@DorisOu
Copy link

DorisOu commented Apr 19, 2019

@matepaiva I have the same problem: and I do not know how to solve this, could you pls send more details about how to solve this? thks.

@cbj4074
Copy link

cbj4074 commented Aug 27, 2019

My situation was similar to the one that @matepaiva describes, with the exception that sending the XHR request header had no effect on the redirection. After wrestling with it for awhile and reading the intelligent discourse on this thread, it became clear that there is no client-side solution to this problem.

Nay; I had to modify the server-side API endpoint to check for X-Requested-With and respond with a lightweight JSON string and 401 HTTP status code if present, rather than a 302 redirection. As soon as I did that, a simple try/catch around the Axios call did the job, and error was caught as expected henceforth.

The bottom-line seems to be: pass X-Requested-With = XMLHttpRequest with the Axios request, and if that doesn't work, the problem is server-side.

@NishantDesai1306
Copy link

NishantDesai1306 commented Oct 10, 2019

I am doing it manually but analyzing the response.request.responseURL in interceptor, basically I wanted to open the login modal when user is unauthenticated and he/she tried to make any API calls.

my express server will do a simple redirect via res.redirect("/login?redirect=...") in that case.

but on client side browser simply fetches the login page and mark the request as completed (which it should), so not in interceptor I am checking this

if (response.request.responseURL && response.request.responseURL.includes("/login?redirect=") {
     // dispatch store action
     // open modal through jquery
    // redirect via location.href
}

@matepaiva
Copy link

@NishantDesai1306 it looks more like a workaround, but if passing X-Requested-With = XMLHttpRequest with the Axios request doesn't solve it, I think you don't have much of a choice.

@axios axios locked and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants