Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch client redirect

I'm executing a fetch request with the FetchClient (through an Aurelia wrapper).

The fetch client has three modes:

Follow, Error and Manual (I'll discard error for this question)

If I use follow, the fetch client automatically follows the redirection. The problem is that my original request uses basic auth. It works for the original request, but the redirected URL fails with those basic auth credentials.

If I use the "manual" mode, it doesn't follow through, but I don't get access to the "Location" of the redirected resource and I can't get the proper URL for the redirected resource.

How can I work around this?

It would either be a mode where I get access to the complete response, or an interceptor that allows me to modify the headers before requesting the redirected resource.

like image 263
Kenneth Avatar asked Nov 07 '22 16:11

Kenneth


1 Answers

You can check the url property of the response when redirected property is true

https://developer.mozilla.org/en-US/docs/Web/API/Response/url

fetch("someUrl")
.then(r=>{
  if(r.redirected){
     const redirectedToUrl = r.url
  }  
}
like image 151
Alexander Taran Avatar answered Nov 10 '22 01:11

Alexander Taran