Monday 30 March 2020

Downloading file from URL

I am trying to download csv file from the response of a get request below:const axios = require('axios'); async function exports() { let initiate_export = await axios({ method : 'get', url: 'https://v3.synccentric.com/api/v3/products', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer API_TOKEN' }, data: { "campaign_id": campaign id, "fields":["asin","upc","parent_asin"], "downloadable": true, "downloadable_type": "csv" } }) return initiate_export } exports().then(async (res)=>{ await get_export.downloadExport(res.data.attributes.url) }) the function 'exports()' initiates the download process. I pass the returned information to the 'downloadExport' function provided below:​const Fs = require('fs') const Path = require('path') const Axios = require('axios') async function downloadExport (exportUrl) { const todaysdate = new Date(); const url = exportUrl const path = Path.resolve(__dirname, 'results', 'Results' + todaysdate.getMonth() + "-" + todaysdate.getDate() + "-" + todaysdate.getYear() + '.csv') const writer = Fs.createWriteStream(path) const response = await Axios({ url, method: 'GET', responseType: 'stream' }) response.data.pipe(writer) return new Promise((resolve, reject) => { writer.on('finish', resolve) writer.on('error', (error)=>reject(error)) }) } module.exports = { downloadExport } I keep getting the following results from downloadExport():{"errors":[{"status":400,"title":"Export job in progress","detail":"You will be able to download when the job is complete"}]} My question is, how do I call downloadExport, only when the file is finished buffering in the browser?The API im using doesnt provide a polling method for the status of the export.I know downloadExport() works because after several minutes i manually pass the 'res.data.attributes.url' to the function and the CSV download correctly.

Submitted March 30, 2020 at 08:51PM by cbassolympics

No comments:

Post a Comment