0

I have the following code:

const URL = 'https://www.tophtml.com/snl/15.mp3';
window.addEventListener('load', function() {
 const button_load  = document.querySelector('.button_load');
 button_load.addEventListener('click', async function() {
  let url = URL + '?nocache='+(new Date()).getTime();
  let arrayBuffer = await fetchData(url, 501, 510);
  console.log('This is the response itself:');
  console.log(arrayBuffer);
 });
});
function fetchData(url, byte_from, byte_to) {
 return new Promise((resolve, reject) => {
  var request = new XMLHttpRequest();
  request.responseType = 'arraybuffer';
  request.open('GET', url, true);
  request.setRequestHeader("Range", "bytes=" + byte_from + "-" + byte_to);
  request.onload = function() {
   resolve(request.response);
  }
  request.onreadystatechange = function() {
   if (this.readyState == this.HEADERS_RECEIVED) {
    console.log('These are the "Response Headers" visible to JavaScript:');
    console.log(request.getAllResponseHeaders());
   }
  }  
  request.send();
 });
}
<button class="button_load">Load</button>

which you can also find here:

https://codepen.io/anon/pen/OBJqZG?editors=1010

My problem is that for some reason, the Content-Range response header is not visible for JavaScript as you can see on the following image:

enter image description here

Any idea of what's going on here?

Thanks!

davidesp
  • 3,743
  • 10
  • 39
  • 77

1 Answers1

2

Any idea of what's going on here?

It's quite simple, content-range is a response header and not a request header. The only response header that can be set—using XMLHttpRequest.prototype.overrideMimeType—from the client side is the content-type.

If it is exposed by the Access-Control-Expose-Headers response header you may retrieve it using:

request.getResponseHeader('Content-Range')
Knu
  • 14,806
  • 5
  • 56
  • 89
  • but is there any way I can get the value of: `Content-Range` by using `JavaScript` once I submit the request?, any workaround or something?. Thanks! – davidesp Sep 28 '18 at 17:15
  • could you please fork my `CodePen.io` and alert the value? thanks! – davidesp Sep 28 '18 at 23:24
  • @davidesp check my update. It's not possible in that case: it's not exposed. Try it out and look at your console. – Knu Sep 28 '18 at 23:25