Skip to main content

Using FormData in scripts


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

  • New Participant
  • 1 reply
  • October 25, 2021

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


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 });

Reply