Skip to content

Instantly share code, notes, and snippets.

@orpheousff8
Created September 3, 2021 14:44
Show Gist options
  • Save orpheousff8/52f1bcf2ddfb8730dd1f5e542597eb00 to your computer and use it in GitHub Desktop.
Save orpheousff8/52f1bcf2ddfb8730dd1f5e542597eb00 to your computer and use it in GitHub Desktop.
read .csv with JS fetch()
const readCsv = async () => {
try {
//read .csv file on a server
const target = `https://SOME_DOMAIN.com/data/csv/[FILENAME].csv`;
//target can also be api with req.query
//get csv-structure-response from a server
//const target = `https://SOME_DOMAIN.com/api/data/log_csv?[QUERY]`;
const res = await fetch(target, {
method: 'get',
headers: {
'content-type': 'text/csv;charset=UTF-8',
//in case you need authorisation
//'Authorization': 'Bearer ' + [TOKEN] //or what you like
}
});
if (res.status === 200) {
const data = await res.text();
console.log(data);
} else {
console.log(`Error code ${res.status}`);
}
} catch (err) {
console.log(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment