【问题标题】:Node.js request to xml document not receiving properly encoded format?Node.js 对 xml 文档的请求没有接收到正确的编码格式?
【发布时间】:2016-01-21 08:22:33
【问题描述】:

我不完全确定为什么,但我收到的数据看起来像 n�F����S�,来自对 xml 格式的 rss 提要的调用。

    exports.search = function(req, res) {
        request.get('https://secret.co/usearch/'+req.params.id+'/?rss=1', function (error, response, body) {
          console.log(body);
          if (!error && response.statusCode == 200) {
            parseString(body, function (err, result) {
              res.json(result);
            });
          }
        });
    };

只是在一个特定的 url 上,我想知道如何解决这个问题并获得正确的 xml?

【问题讨论】:

  • 您能否将导致您遇到问题的网址添加到问题中?
  • https://kat.cr/usearch/scarface/?rss=1

标签: xml node.js parsing


【解决方案1】:

有问题的 URL 提供 gzip 编码的内容。在请求调用中添加选项gzip : true 将解决问题:

exports.search = function(req, res) {
    request({ method : 'GET', url: 'https://kat.cr/usearch/scarface/?rss=1', gzip: true }, function(error, response, body) {
      console.log(body);
      if (!error && response.statusCode == 200) {
        parseString(body, function (err, result) {
          res.json(result);
        });
      }
    });
};

【讨论】:

  • 看到这个会做什么?在 chrome 开发者工具里面?
  • 在网络选项卡下的 chrome 开发工具中,您会找到一个内容编码列,其中显示“gzip”用于 gzip 响应。我建议使用 curl 来调试这类低级 HTTP 问题,因为 Chrome 对 HTTP 响应有很多魔力。
猜你喜欢
  • 2016-07-24
  • 2014-08-26
  • 2021-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-22
  • 1970-01-01
  • 2019-02-17
相关资源
最近更新 更多