I have an endpoint that is supposed to make a call to the WhatsApp API with the Media URL just like they said to do here: https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media#download-media, and just return it the same way it was returned from whatsapp as well as save the file for later use.
exports.downLoadMedia = async (req, res) => {
const url = req.query.url;
request.get(
{
url: url,
headers: {
Authorization: `Bearer ${process.env.AUTH_TOKEN}`,
Accept: 'audio/ogg',
},
encoding: null, // Set encoding to null to receive binary data
},
function (err, resp, body) {
if (err) {
console.log("Error:", err);
res.status(500).json({ error: "An error occurred." });
} else {
const fileName = 'downloaded_audio.ogg';
const filePath = path.join(__dirname, fileName); // Construct file path
fs.writeFile(filePath, body, 'binary', (writeErr) => {
if (writeErr) {
console.log("Error writing file:", writeErr);
res.status(500).json({ error: "An error occurred while saving the audio." });
} else {
// Use res.download() to send the saved file as a download response
res.download(filePath, 'downloaded_audio.ogg', (downloadErr) => {
if (downloadErr) {
console.log("Error sending download:", downloadErr);
res.status(500).json({ error: "An error occurred while sending the download." });
}
});
}
});
}
}
);
};
when i make a get request in Postman with the WhatsApp Media url directly it shows up like this:
and the audio plays as it should.
However, when i make a request to my endpoint above it shows up like this:
The audio is saved but it does not play.
Basically, my endpoint should be the proxy to get the audio/voice note file.