2

I am trying to access the api but for accessing the api I need token so When I try to hit the api it redirects to the auth page.

I can see the redirection is happening in the network tab.

is there any way to prevent this?

I wanted to get the 302 error code when I first hit the api(its giving 302 but with fetch/Redirect) but because of automatic redirection its throwing error and going to error block always.

fetch(url, { method: 'POST', redirect: 'error'})
.then(response => {
    // handle 302 response
})
.catch(function(err) {
    console.info(err);
});

Edit: To get the token I need to redirect user to the auth page. so once I get the 302 error Ill redirect the user to the auth page.

Joomler
  • 2,610
  • 3
  • 30
  • 37
  • First need to get the token from the Auth API. Then store this token & use with other api calls. – A G May 10 '22 at 22:00
  • Maybe get a token? It's not really clear what you're asking. – Andy May 10 '22 at 22:00
  • @Take_Care_ I already tried that but still no luck – Joomler May 10 '22 at 22:13
  • Sorry I was thinking that you want to get error :P and now I see You don't want it to happen and just get the initial response without following the redirection – Take_Care_ May 10 '22 at 22:16
  • @Take_Care_ bingo :) – Joomler May 10 '22 at 22:18
  • 1
    I am not sure but this can be impossible https://fetch.spec.whatwg.org/#atomic-http-redirect-handling – Take_Care_ May 10 '22 at 22:22
  • 3
    By design the fetch API doesn’t expose redirects to frontend code. Browsers automatically follow the redirects, and there’s no way to catch them in your frontend code. See the answer at https://stackoverflow.com/a/46031800/441757. So the only way to do what’s described in the question is to not make the request from frontend code but instead make the request from backend code, and consume the response there and then pass something on to your frontend code. – sideshowbarker May 10 '22 at 22:45

0 Answers0