Help

Inport a CSV through Fetch

Topic Labels: Automations
790 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Rod_Maxwell
4 - Data Explorer
4 - Data Explorer

I’m completely new to Airtable and looking for a way to automate a daily download of a CSV file.

After some digging around I have the following script:

let accountnumber = “9402”;
let cdrdate = “2021-05-20”;
let username = “MY_USERNAME”;
let password = “MY_PASSWORD”;
// the request url
let url = “https://portal.overthewire.com.au/voice/cdrs/"+accountnumber+"/”+cdrdate;
// the credentials
let authString= b2a(username+":"+password);
let options = {
“method”: “GET”,
“headers”: {
“Content-Type” : “application/json”,
“Authorization” : "Basic " + authString
}
}
response = await fetch(url, options);
console.log(response);

function b2a(a) {
var c, d, e, f, g, h, i, j, o, b = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=”, k = 0, l = 0, m = “”, n = ;
if (!a) return a;
do c = a.charCodeAt(k++), d = a.charCodeAt(k++), e = a.charCodeAt(k++), j = c << 16 | d << 8 | e,
f = 63 & j >> 18, g = 63 & j >> 12, h = 63 & j >> 6, i = 63 & j, n[l++] = b.charAt(f) + b.charAt(g) + b.charAt(h) + b.charAt(i); while (k < a.length);
return m = n.join(""), o = a.length % 3, (o ? m.slice(0, o - 3) :m) + “===”.slice(o || 3);
}

If I test the script it executes without error and I get the following console output:

{type: “basic”, url: “https://portal.overthewire.com.au/voice/cdrs/9402/2021-05-20”, status: 200, statusText: “OK”, ok: true…}

which suggested that that the “get” call was authenticated and successful, however I would have thought that the variable ‘response’ contains my CSV data.

How do I get to the CSV data?

Any help much appreciated.

1 Reply 1
Raminder_Singh
7 - App Architect
7 - App Architect

Hi @Rod_Maxwell,

You can get the text returned in the response by calling the text() method on the response object like this: response.text(). There are other methods on the response object, e.g. a json() method to get the result as a json. Refer to this page to see what else is available.

Since your data is in CSV format, the text() method will return just a string which needs to be parsed as CSV. One example of how to do that is on this StackOverflow page.

HTH
-Raminder