简体   繁体   中英

Why do axios calls via nodejs scripts include Content-Length Header while browser requests don't?

I am sending both a HEAD and GET to a resource to find out the size (compressed and uncompressed gzip).

I found that when running axios through a nodejs script like this:

var axios = require('axios');

axios.head('<URL_TO_RESOURCE>')
  .then(function (response) {
    console.log(response.headers['content-length']);
  })
  .catch(function (error) {
    console.log(error.request.res.statusCode);
    console.log(error.request.res.headers['content-length']);
  });

I receive the Content-Length Headers (also with a axios.get).

However, when running a similar call on the browser (through axios CDN script import), this header is missing.

Why is that and is there a way to get that header in the browser?

I noticed this problem as well with request and other libraries.

Adding the response header Access-Control-Expose-Headers:Content-Length on the server side did the trick.

The browser actually stripped away that header, that is the reason why a node.js script worked, while frontend javascript didn't. Also, this issue is not related to axios or any other request library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM