befreeforlife
befreeforlife

Reputation: 511

Read xml file from url in node.js: encoding problem

I would like to read xml file from url using next code:

  var request = require("request");
  request.get(
    "http://regnskaber.virk.dk/27946272/ZG9rdW1lbnRsYWdlcjovLzAzLzMwLzllL2M3L2Y5LzUxYzQtNDZmNi04YzliLTdhODg1ODA0ZTdlNA.xml",
    function (error, response, body) {
      if (!error && response.statusCode == 200) {
        console.log(body);
        // Continue with your processing here.
      }
    }
  );
 

In output I got like next:

�������\D���<a>��4E��hQ�:!B��lu���u�ݶ�~^�Q�=<~��~ ���tq��#FUE+k���զj��_+��aNF�V�)M��E�O؍��V�c���c��r�n��U�����3����:�U���Fa�>�Qa\���+�����������W�;�^�FEi���F���Ū\W�9�
�������M����䯇��+�e����uvr\yR�P��mM�*��Ժ��6��^1>m�U����OV�a@ݣ8��        �3����f�>�Pp\��?���Nj�Nj����rqNZ�W[�;���O��Uw2\�O��.M�>e���4Ǵ����?F.��ώ�A;�P��oG��mS�|~ss��,(Y��JX�qJD����&W��,a��n���H��T��*Պ�an�u!&�T�R�VZ����Z����`�Y�a�

It seems that problem with encoding but I can't identify the encoding and fix it. In browser xml output is correct

Upvotes: 1

Views: 617

Answers (1)

Trott
Trott

Reputation: 70123

Using curl to hit your endpoint gets garbled data instead of XML as well. Looking at the headers, content-encoding is set to gzip. So this worked for me:

var request = require("request");
  request.get({
    method: 'GET',
    url: "http://regnskaber.virk.dk/27946272/ZG9rdW1lbnRsYWdlcjovLzAzLzMwLzllL2M3L2Y5LzUxYzQtNDZmNi04YzliLTdhODg1ODA0ZTdlNA.xml",
    gzip: true},
    function (error, response, body) {
      if (!error && response.statusCode == 200) {
        console.log(body);
        // Continue with your processing here.
      }
    }
  );

Upvotes: 2

Related Questions