1

I am calling one rest API which is returning pdf file. However, it is showing as blank when i open it. Through console it seems data is getting corrupted.


let url ="API-URL";
  var options = {
    'method': 'GET',
    'url': url ,
    'id': '50d55684-9c61-4843-b023-1a9270ff5768',
    'format': 'pdf',
    'encoding': 'binary',
    'headers': {
      'Authorization': auth,
    },
    
    
  };
  process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
  // Return new promise 
  return new Promise(function(resolve, reject) {
    // Do async job
      request.get(options, function(err, response) {
          if (err) {
              reject(err);
          } 
          else {
            console.log(typeof response.body);
            fs.writeFile("./test.pdf", response.body, 'binary', (err) => {
              if(err) {
                  return console.log(err);
              }
              console.log("The file was saved!");
          }); 
            resolve(response.body);
            
          }
      })
  })

I followed one of solution provided to set encoding as binary or null. But both didnt work for me.

The pdf data looks like below

%PDF-1.4
%����
4 0 obj
<</Subtype/Form/Filter/FlateDecode/Type/XObject/Matrix[1 0 0 1 0 0]/FormType 1/Resources<</ColorSpace<</CSpg/DeviceGray/PCSp 5 0 R/CSp/DeviceRGB>>/ExtGState<</GSa 6 0 R/GState99 7 0 R/GState136 8 0 R/GState137 9 0 R/GState138 10 0 R>>/Pattern<<>>/Font<<>>/XObject<</Im49 11 0 R/Im29 12 0 R/Im126 13 0 R/Im41 14 0 R/Im85 15 0 R/Im102 16 0 R/Im21 17 0 R/Im65 18 0 R/Im45 19 0 R/Im89 20 0 R/Im106 21 0 R/Im25 22 0 R/Im69 23 0 R/Im130 24 0 R/Im81 25 0 R/Im134 26 0 R/Im61 27 0 R/Im110 28 0 R/Im17 29 0 R/Im53 30 0 R/Im97 31 0 R/Im33 32 0 R/Im77 33 0 R/Im114 34 0 R/Im13 35 0 R/Im57 36 0 R/Im37 37 0 R/Im118 38 0 R/Im9 39 0 R/Im93 40 0 R/Im122 41 0 R/Im73 42 0 R>>>>/Length 450365/BBox[0 0 792 612]>>stream
x��͎,=���O�k�r��Nr]0
�����p��@���R/���f��sY���e|�U��<t�h��Ϳ��y����������}������������ߎ�����ox�÷�}������?|��l������~3�6����?^����������/�����|��_�0���o5ٿ?��\���5���������K�_����_�_�����?����Ֆ�����?:����=���鯼��^�~����MX�K_��nº  �&���nº �%�Ŷ�׼��v��qoǽ�v���&w��RƗ~���nº  �&���n�z    �z���5���v��qoǽ�v�������MX�K_��nº    �&���nº �%�Ŷ�׼��v��qoǽ�v��qo��o��nr7a�.e|�w�  �&���nº �&�� �ێ_�n��qoǽ�v��qoǽ/���݄������&���nº    �&���^��^l;~ͻyoǽ�v��qoǽ�v��qoǽ�v��qoǽ�v��qo�m�e    �KSƗ~���nº  �&���n�z    �z���5���v��qoǽ�v��qoǽ�v�ݦ[�N{x�׿?�ߴ�6�{�r:՟4��7��>��o�<����~������n�[?��rT�x=��������_g|E��6����~�_�?~|��h���~���W~\������6�_�d��c��ӿu��x���ٮ �����o����?w&�_6����]}����;������8o���?�����?�;>�����^�>��x�����~����}���������_L�g�x��_���r��������#���������O����[�\P��'����|�|�=����������_�k0��'x��`��G,�����u�}ԣ.�o��ſ�
Nl�=��۷�Ɍ����+D�����KJ6���o?�6��s
'�$��/'��I�����Ӈ�Ye��J�YrVY�c���ח�nj�u��ڎ���j��*y-_/�Z:>�{�J��ty.]y߷\�1����zɩ��Џ7��'���WN_���X�K�Ŷq�X:|��q,�6v��o>g[:�����T�KI�v-����k�s�ƾo��S���J���|��Ԝ|��W�w��7��}N���w�m������<�'n�_ƣ���{�>w���-����r�O��K꽼������n����=��;�3㏿.C1� _����{󿿓�w��M_�����������{yo�w߯7}���}�~�ɽ�������z��}����כ���{yo�����c�_:$�KO�������;݋�^�>~*�������1�׿��E�����4��B��<���y�+G�����a>���L�����7?�Ư!0���w������W��7_�|���ٽ�f�7���}voy3��I����%Ʉ�Vb�"$����/v��w��ދ|3ŗ������Oe���E�L�����7S��ޛʾ��W�9/0��?��"�L�zo*�Tv_�_�0��_��ދ|3ŗ�z_�A���z�)��ѿ��w��� �{3țA~��K���W>��"���o��>�7���� oy�ݛA~�E��� o��g�^�A� �7���bw��}^���-��3�_��~S�/Oe���E�L�����7S��ޛʾ��W�9/0��?��"�L�zo*�Tv_�_�0��_��ދ|3ŗ�z_�A���z�)��ѿ��w��� �{3țA~�NP���W>��"���o��>�7���� oy�ݛA~�E��� o��g�^�A�  �7���bw��}^���-��3�_��~S�/Oe���E�L�����7S��ޛʾ��W�9/0��?��"�L�zo*�Tv_�_�0��_��ދ|3ŗ�z_�A���z�)��ѿ��w��� �{3țA~�NP���W>��"���o��>�7���� oy�ݛA~�E��� o��g�^�A��f��ٽ�� oy3������-�� oy3��<��"��f�7����� oy3țA�g�f�_n�oy3țA��ٽ�f�w��L�'����;%��^�)�DՓ��~�Tv_�_�0��_��ދ|3��꽩�KP�}�~���S|��z/������/Ae���E�L�����7S|�����������{�o�zG��>�7���@�2�7?��G�^���-0�g�f�7���� �{3�/��7���� _��ދ|3�;A�&��R�X~˻��n��?�v�ne����?��{ߝ���z/��_"^즲_?��W�9/0��?��"�L�zo*�Tv_�_�0��_��ދ|3��꽩�KP�}�~���S|��z/��_��}1������G�^䛿�т_�A�"b��z��v�E��/���}�����'A��_�ދ|�ÿ5?�Wߏ����������x�~v���'��g���V��'~{o��Z�Z.�����f�c��ogZ���������ʱ�#��1���������/4���~+���u��~��,]���>�.�'
-���EX���d?8�8w�u����=}��|��}?�ftfh\��p>F=v����-���EC��4Tް�<7Cۜ���?rO7;�c���W+��8�-���e���9�<�ް�h��=����S�1��$[�k��?�t�c�k�mal���K���>�����������1���1�r��o2�8��+=]���a�yĚ+���>�Q���>��~�51��[`�O�0t�`hL0�X��6�/��q�c�t�ن64&��\h#o����c���c}]5���L�e���c���Y/Oz96�q{��5�zě�����J�o~X�m��zq^/��=�JO�6t����t�W,�ؿ�}�u|��c  �`/��,����2_��Msu����O������6�({�'�B�x���
��u��8�[�p:J�#ah�{g�n��1��uJ��v�����Ntr��f·��>_عEI�]��T����X_�C�O������C0�����

Can someone help me what is the mistake i am doing ?

2 Answers 2

1

Sample solution using Node.js' fs promises and request-promise.

const fs = require('fs').promises,
      request = require('request-promise');

let fileName = './test.pdf',
    options = {
      'method': 'GET',
      'url': ...,
      'encoding': null, // response should be buffer
      ...
    };

request(options)
  .then(response => fs.writeFile(filename, response))
  .then(() => console.log("The file was saved!"))
  .catch(err => console.warn(err));
Sign up to request clarification or add additional context in comments.

Comments

0
let options = {
  method: "GET",
  url: "your Url"
  responseType: "arraybuffer",
  responseEncoding: "binary",
  headers: {
    "Content-Type": "application/pdf",
    "Authorization": "Bearer " + accessToken
  }
};

fs.writeFileSync("D://*******//test.pdf", response.data);
fs.close();

This worked for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.