Help

Re: Using FormData in scripts

1156 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Tom_Beckenham1
4 - Data Explorer
4 - Data Explorer

Hi,

I’m getting an error when I try to use create a Javascript FormData object in preparation for posting to an API. I would have thought FormData would have been well supported.

console.log(`Hello, ${base.name}!`);
const endPoint = new URL('https://myserver.com/jsonAPI/');
const formData = new FormData();
formData.append("user", 'tom');

const response = await fetch(endPoint.toString(), {
    method: "POST",
    body: formData,
});

console.log(response)

Getting an error saying that FormData is not defined

2 Replies 2
Thomas_Brady
4 - Data Explorer
4 - Data Explorer

They mention in their docs that the FormData API is not available in Airtable Scripting: Airtable Scripting

Titouan_Parand
5 - Automation Enthusiast
5 - Automation Enthusiast

To use formData you can build the object yourself using:

let data = {
    key1: 'value1',
    key2: 'value2'
};

// Encoder les paramètres en format x-www-form-urlencoded
let formData = Object.keys(data).map(key => encodeURIComponent(key) + '=' + encodeURIComponent(data[key])).join('&');

await fetch('URL_DE_VOTRE_API', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
    },
    body: formData
});