1

i'm trying to download pdf files from an api using the requst js module. i've set up a test route on express that fetches the file and send it back to the user.

let url = 'http://www.workagreements.economy.gov.il/Agreements/' + String(agrNum) + '.pdf';

let options = {
  url: url,
  method: 'GET',
};

request(options, function(err, response, body) {
  let buff = Buffer.from(body, 'utf8');

  res.attachment(String(agrNum) + '.pdf');
  res.status(200).send(buff)
});

the file recieved is in the wrong encoding, resulting in a blank pdf file (with the correct number of pages). it seems that the problem starts with the encoding of the output of the request module. i've tried variuos encoding options for the buffer, but it didn't solve the problem. any ideas?

Roip
  • 81
  • 2
  • 6
  • refer link, https://stackoverflow.com/questions/10046039/nodejs-send-file-in-response or https://stackoverflow.com/questions/25463423/res-sendfile-absolute-path – chetan mekha Nov 26 '17 at 10:26
  • You should consider the file as binary, and not apply any encoding. – jcaron Nov 26 '17 at 14:40
  • the file is also unreadable if i just send 'body' (=the response to my request) without turning it into a buffer or encoding it. – Roip Nov 26 '17 at 18:00
  • Possible duplicate of [Getting binary content in Node.js using request](https://stackoverflow.com/questions/14855015/getting-binary-content-in-node-js-using-request) – GilZ Nov 27 '17 at 09:32
  • i've tried setting encoding to null, result was the same. the solution i found was using the node js http module and saving the response to a file stream of a temp file. – Roip Nov 28 '17 at 09:51

0 Answers0