1
votes

I have a response from an ebay-api

--MIMEBoundaryurn_uuid_C91296EA5FF69EE9571479882375576565344 Content-Type: application/xop+xml; charset=utf-8; type="text/xml" Content-Transfer-Encoding: binary Content-ID: <0.urn:uuid:C91296EA5FF69EE9571479882375576565345>

Success1.1.02016-11-23T06:26:15.576Z514 --MIMEBoundaryurn_uuid_C91296EA5FF69EE9571479882375574545344 Content-Type: application/zip Content-Transfer-Encoding: binary Content-ID:

PKY'uIi[��@�50014028337_report.xmlUT y�2Xy�2Xux 00�R�j�@��+��[��PlX#�(�x,=l�q]Lfewc��w Ĥ��O��١�HT���t��GGT� ��6�;���'������.$����=d����m;c}Wߦ�RW�A f�����g�I��4U��x��3��f���ғ{f��xj�,+���ۖI%5��B's��G,#��t,L{�c�����MD笓��)!�9�� �M�o;8_��<�i�y����sz���u���=��Ջ^2�S��%+2�2�`QV�$�����~?�w�ǥ�_Q�퉦�'PKY'uIi[��@���50014028337_report.xmlUTy�2Xux 00PK\� --MIMEBoundaryurn_uuid_C91296EA5FF69EE9571479882375576565344--

This is of type string. and i extracted the attached zip file data i.e.

PKY'uIi[��@�50014028337_report.xmlUT y�2Xy�2Xux 00�R�j�@��+��[��PlX#�(�x,=l�q]Lfewc��w Ĥ��O��١�HT���t��GGT� ��6�;���'������.$����=d����m;c}Wߦ�RW�A f�����g�I��4U��x��3��f���ғ{f��xj�,+���ۖI%5��B's��G,#��t,L{�c�����MD笓��)!�9�� �M�o;8_��<�i�y����sz���u���=��Ջ^2�S��%+2�2�`QV�$�����~?�w�ǥ�_Q�퉦�'PKY'uIi[��@���50014028338_report.xmlUTy�2Xux 00PK\�

This shows that it has a report.xml in it. So when i write this data in a zip file, it creates a zip file and upon extract gives error.

fs.writeFile("./static/DownloadFile.zip", fileData, 'binary', function(err){
                  if (err) throw err;
                  console.log("success");
                  });

How can i write this data in a zip file properly. Pls advice. If required any more information.

EDIT: I tried writing the zip file in PHP and is succssfully writing it with this code:

$zipFilename="DownloadFile.zip";
        $data       =   $fileData;
        $handler = fopen($zipFilename, 'wb')
            or die("Failed. Cannot Open $zipFilename to Write!</b></p>");
        fwrite($handler, $data);
        fclose($handler);

Please advice how can i achieve the same thing in nodejs.

2

2 Answers

4
votes

Depending on what HTTP Client you are using the implementation might change a little.

With axios I'm doing something like so:

  • I'm requesting a zip file so I specify the Accept header as application/zip
  • In order to get a buffer and not Binary, specify the responseType as arrayBuffer
const res = await axios.get('/routToThat/file', {
  headers: {
    Accept: 'application/zip',
  },
  responseType: 'arraybuffer',
});

By doing the latter, instead of receiving a Binary from the response:

A@B�ArE⏾�7�ϫ���f�걺N�����Yg���o_M^�D�T�U X_���e?� hi\...

I receive a Buffer:

Buffer(22781691) [80, 75, 3, …]

Once the request is resolved and I have that Buffer, I use that same writeFile function from fs

NOTE: I'm not specifying the Encoding in writeFile

fs.writeFile(name, res.data, (err) => {
  if (err) throw err;
  console.log("success");
});
1
votes

As I see in your code example your binary data is already mangled by request module. Just use in request setting

encoding:null

and the zip file is a valid binary in body (now buffer instead of utf-8 string!) you can decompress. As long as you see the questions marks you still have the encoding issue.