apigoogle-apps-scripturlfetchairtablehttp-status-code-422

Post method via Airtable API using a Google Script


I have the following code in a Google Script:

var data = {
    "records": [
          {
          "fields": {
              "Contract Address": "test",
              "0x8df3aad3a84da6b69a4da8aec3ea40d9091b2ac4": "1234"
          }
          }
    ]
};

var options = {
  "method" : "post",
  'Content-Type': 'application/json',
  'muteHttpExceptions' : true,
  // "payload" : data,
  "payload" : JSON.stringify(data)
};

function tryAPost(){
  var url = "https://api.airtable.com/v0/xxxxxxxxxxxxx/Balance%20Tracking?api_key=keyxxxxxxxxxx";
  var response = UrlFetchApp.fetch(url, options);
  //console.log(response.getContentText());
 console.log(response.getResponseCode());
};

I get the following response:

422

And the data does not end up in Airtable.

The payload works in the body of a post request in Postman.

What am I doing wrong?

EDIT Per Comment:

here's the exmaple code from airtable:

var Airtable = require('airtable');
var base = new Airtable({apiKey: 'YOUR_API_KEY'}).base('xxxxxxxxxxxx');

base('Balance Tracking').create([
  {
    "fields": {
      "Contract Address": "Thu, 03 Feb 2022 15:12:37 GMT",
      "0xfecf784f48125ccb7d8855cdda7c5ed6b5024cb3": 12055358359168

Adding postman screenshot per comment: enter image description here

enter image description here


Solution

  • I asked in the Airtable Forum and someone came up with a solution that worked.

    Here's the link.

    Here's the answer:

    //
    // post to Airtable (universal)
    //
    function atPostTable_(baseKey, tableName, payload)
    {
      var options = 
          {
            method: 'POST',
            headers: {
              'Authorization' : 'Bearer ' + cMyAirtableAPIKey,
              'Content-Type'  : 'application/json'
            },
            payload : JSON.stringify(payload),
            muteHttpExceptions : true,
            followRedirects: true
          };
      var response = UrlFetchApp.fetch(cAirtableAPIEndpoint + baseKey + "/" + encodeURIComponent(tableName), options).getContentText();
      return(response);
    }