Tomasz Pluskiewicz
Tomasz Pluskiewicz

Reputation: 3662

Accessing response headers on CORS request

I'm using the fetch API to make a cross-domain request similar to the below snippet

window.fetch('http://data.test.wikibus.org/magazines', { method: 'get'})
      .then(function(response) { 
        var linkHeader = response.headers.get('Link');
        document.querySelector('#link-header').innerText = 'The Link header is: ' + linkHeader;
      });
<span id="link-header"></span>

As you see the Link header (and some other headers too) is not accessible although it is returned in the response. I assume that's a CORS issue, because on local requests all headers are accessible.

Is that by design? Is there a way around that problem?

Upvotes: 4

Views: 3141

Answers (1)

Anne
Anne

Reputation: 7643

The resource you are requesting most likely lacks a Access-Control-Expose-Headers header that contains Link as value.

See https://fetch.spec.whatwg.org/#http-access-control-expose-headers and see https://fetch.spec.whatwg.org/#concept-filtered-response-cors for the details of which headers get filtered out of a CORS response.

Upvotes: 6

Related Questions