Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Response headers not available for fetch request with 'redirect: manual'

I am doing

        console.log("navigating");
        var rsp = await fetch(params.url, {
            credentials: "include", redirect: "manual", mode: "cors"
        });
        console.log(rsp);
        rsp.headers.forEach(console.log);

        console.log(rsp.headers.get('Location'));
        console.log(rsp.headers.get('location'));

and the reponse headers from chrome dev tools:

Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://localhost:4400
Access-Control-Expose-Headers: Location
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 0
Date: Fri, 05 Oct 2018 12:48:21 GMT
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Location: http://localhost/test

gives

Response 
body: (...)
bodyUsed: falseheaders: 
Headers {}
ok: false
redirected: false
status: 0
statusText: ""
type: "opaqueredirect"
url: "..."

index.ts:161 null
index.ts:162 null

Is it not possible to get response headers out on redirect response?

like image 923
Poul K. Sørensen Avatar asked Oct 05 '18 12:10

Poul K. Sørensen


People also ask

Does fetch follow redirects?

Normally, fetch transparently follows HTTP-redirects, like 301, 302 etc.

What is Opaqueredirect?

An opaque-redirect filtered response is a filtered response whose type is "opaqueredirect", status is 0, status message is the empty byte sequence, header list is empty, body is null, and trailer is empty.


Video Answer


1 Answers

Is it not possible to get response headers out on redirect response?

No, it’s not possible. The requirements in the Fetch spec prevent it.

What the question shows is expected for manual redirect mode: The headers object exposed to frontend JS is expected to be empty in responses to redirect: "manual" requests.

When a request sets manual redirect mode, the response type is opaqueredirect. Info on the effects of that is at https://developer.mozilla.org/en-US/docs/Web/API/Response/type:

opaqueredirect: The fetch request was made with redirect: "manual". The Response's status is 0, headers are empty, body is null and trailer is empty.


Those details in that MDN article are based directly on the following parts of the Fetch spec:

https://fetch.spec.whatwg.org/#concept-request-redirect-mode

A request has an associated redirect mode, which is "follow", "error", or "manual".

"manual": Retrieves an opaque-redirect filtered response when a request is met with a redirect so that the redirect can be followed manually.

https://fetch.spec.whatwg.org/#concept-filtered-response-opaque-redirect

opaque-redirect filtered response is a filtered response whose type is "opaqueredirect", status is 0, status message is the empty byte sequence, header list is empty, body is null

an opaque filtered response and an opaque-redirect filtered response are nearly indistinguishable from a network error

like image 158
sideshowbarker Avatar answered Oct 16 '22 20:10

sideshowbarker