Created
September 3, 2021 14:44
-
-
Save orpheousff8/52f1bcf2ddfb8730dd1f5e542597eb00 to your computer and use it in GitHub Desktop.
read .csv with JS fetch()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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