css - https请求响应css文字乱码

标签 css node.js rest https

我正在尝试获取 CSS 样式表,但由于某种原因,响应都是乱码。是编码错误吗?响应 header 表示编码只是 UTF-8。

var https = require('https');
var fs = require('fs');

var url ='https://d14pr3cu5atb0x.cloudfront.net/pkg/css/chimera-e6f0150f64.css';

var request = https.request(url, function (res) {
    var data = '';
    res.on('data', function (chunk) {
        data += chunk;
    });
    res.on('end', function () {
        console.log(data);
    });
});
request.on('error', function (e) {
    console.log(e.message);
});
request.end();

输出:

�{��6�7���zk+�8e�.�Ԟڱ�8�m�v�ٓrQ%1#�
�g�n:��|�쎈��Y�˗H�,-�"��?�(�f������:�]&Ep�.�h��_��'�t͓��,�+R�{w��Z�f
���-A���<n�E�ϫ8;N>9�ҢH�g�ps�%�uq����*�y��?�
                                           ۝�\��y�/WYz�-��4;��r����E=!�$<i}�$0�ZK��:�ǻ~*�;�/�˻A�I�kVC>Z�P�����Ԛ2��|�/�Qvy�e��؁�L�Lf�Ye��Ul�e��U}1i�FȤHŃ���N������҆'()�*X���_���h.�#TG&7ς8�u���M�
                                                                                                                                                                                               Lq�ML6ZI:D���f$�Ԓ�%���y�<�]A�
&l6�M�A^�n�]�����A��V��]tX$i{��}��JE���b����ԯ�}LX�}R���0�^�6?U�����\4���:�����8hf�*����T�2�a;?E�&׳M��<�h�ӻ�Is'��e��"�o�v�#�^�E�l��2Yͣ}��;��!���4-⬽��L���ov�     ]~�"xu{\$�~ݞ�6���Q��
�D��y�R�9�r7; t�k�"��n(h��h�ln�r4�y�%�;�xvlӏ�2�rdK;4-��jr���b��V��Y��-�m���W��D�Y��")6�o,c��yw�Z�?���b�c�U$E�I�w�&�g
                                                                                                                    �9��0
                                                                                                                         ���6��H���Ռ�����Vx7O�]�$1��{E�m�Ksdw(  �{�AN|2OYg�]|��-H�%��;T��z�q@R����:����HR���

最佳答案

我从未使用过 node.js,但对我来说,数据似乎是经过压缩的,并且基于此处的文档:https://www.npmjs.com/package/request看起来您需要启用 gzip 才能获得正确的响应。以下是文档设置 gzip 中生成正确数据的示例的复制和粘贴:

var url ='https://d14pr3cu5atb0x.cloudfront.net/pkg/css/chimera-e6f0150f64.css';
var request = require('request')
  request(
    { method: 'GET'
    , uri: url
    , gzip: true
    }
  , function (error, response, body) {
      // body is the decompressed response body
      console.log('server encoded the data as: ' + (response.headers['content-encoding'] || 'identity'))
      console.log('the decoded data is: ' + body)
    }
  )
  .on('data', function(data) {
    // decompressed data as it is received
    console.log('decoded chunk: ' + data)
  })
  .on('response', function(response) {
    // unmodified http.IncomingMessage object
    response.on('data', function(data) {
      // compressed data as it is received
      console.log('received ' + data.length + ' bytes of compressed data')
    })
  })

关于css - https请求响应css文字乱码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49914968/

相关文章:

javascript - 自动滑出导航加载?

html - 将按钮放在列表的右下角

javascript - 为什么我的 firebase 网站无法显示图像 block ?

javascript - 无法使用nodejs的pdf2json处理多个pdf文件

rest - 如何捕获 Symfony2 Controller 操作中可能出现的所有异常?

html - 最小化 html 和 css 时网页崩溃

javascript - 使用 exec() 在 PHP 中执行 nodejs 脚本文件

node.js - npm install 卡在 Node lib/install.js 上

python - 如何将 Tavern 测试的响应保存在 JSON 文件中?

Java拉力赛休息API : How to add new user to the Project