Node (Express) - Trying to save a PDF from an API call Node (Express) - Trying to save a PDF from an API call express express

Node (Express) - Trying to save a PDF from an API call


Your code doesn't work because the underlying request library (used by request-promise) requires the option encoding set to null for binary data - see https://github.com/request/request#requestoptions-callback.

Here's how you download binary data using that module -

app.post("/getPayslipURL", function(client_request, res) {    const NI_NUMBER_REQUEST = db_api.createRequestTemplate({        body: JSON.stringify(client_request.body),        encoding: null    });    requestPromise(NI_NUMBER_REQUEST)        .then((db_response) => {               const PAY_API_OPTIONS = /*Code to generate options based on furhter DB info (Includes dates etc)*/            return requestPromise(PAY_API_OPTIONS); // Call pay API        })        .then((pay_pdf_data) => {             fs.writeFile("./test.pdf", pay_pdf_data, 'binary', (err) => {                if(err) {                    return console.log(err);                }                console.log("The file was saved!");            });         })        .catch(err => `Error caught: ${console.log}`) // Catch any errors on our request chain    });}