简体   繁体   English

为什么通过nodejs脚本进行的axios调用包含Content-Length Header,而浏览器请求却没有?

[英]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). 我将HEAD和GET都发送到资源以找出大小(压缩和未压缩的gzip)。

I found that when running axios through a nodejs script like this: 我发现通过像这样的nodejs脚本运行axios时:

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). 我收到了Content-Length标头(也包含axios.get)。

However, when running a similar call on the browser (through axios CDN script import), this header is missing. 但是,在浏览器上运行类似调用(通过axios CDN脚本导入)时,此标头丢失。

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. 我在request库和其他库中也注意到了这个问题。

Adding the response header Access-Control-Expose-Headers:Content-Length on the server side did the trick. 在服务器端添加响应标头Access-Control-Expose-Headers:Content-Length问题。

The browser actually stripped away that header, that is the reason why a node.js script worked, while frontend javascript didn't. 浏览器实际上剥离了该标头,这就是为什么node.js脚本起作用而前端javascript却不起作用的原因。 Also, this issue is not related to axios or any other request library. 此外,此问题与axios或任何其他请求库无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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