I have a react app that sends a POST request to my rails API. I want my API endpoint to generate a csv file and then to send back that file to the react app. I want then the browser to download of the csv file for the end user.
Here's how the endpoint looks like :
def generate
// next line builds the csv in tmp directory
period_recap_csv = period_recap.build_csv
// next line is supposed to send back the csv as response
send_file Rails.root.join(period_recap.filepath), filename: period_recap.filename, type: 'text/csv'
end
On the front end side here's how my request looks like :
export function generateCsvRequest(startDate, endDate) {
fetch("http://localhost:3000/billing/finance-recaps/generate", {
method: "post",
headers: {
Authorisation: `Token token=${authToken}`,
'Accept': 'text/csv',
'Content-Type': 'application/json',
'X-Key-Inflection': 'camel',
},
//make sure to serialize your JSON body
body: JSON.stringify({
start_date: startDate,
end_date: endDate
})
})
.then( (response) => {
console.log('resp', response);
return response;
}).then((data) => {
// what should I do Here with the ReadableStream I get back ??
console.log(data);
}).catch(err => console.error(err));
}
As the response body I get a readableStream :
What should I do now with that ReadableStream object to launch the download of that CSV file on the end user browser ?