0

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

DBWeinstein
  • 8,605
  • 31
  • 73
  • 118
  • You need to provide us some documentation as the api docs don't seem to be in the public. Try `"payload" : JSON.stringify(data)` – TheMaster Feb 05 '22 at 16:14
  • I added the example code from airtable to the question. I tried ```JSON.strigify(data)``` and it didn't work. – DBWeinstein Feb 05 '22 at 16:23
  • It's JSON.stringify(object) – Cooper Feb 05 '22 at 17:00
  • You need to at least show the postman screenshot. Also, you should log the response as text: `console.log(response.getContentText())` – TheMaster Feb 05 '22 at 19:32
  • @Cooper I'm sorry to be so thick, but I thought ```data``` was an object. – DBWeinstein Feb 05 '22 at 19:48
  • @TheMaster I added ``` console.log(response.getContentText());``` and a screenshot from Postman. Is that what you're looking for? – DBWeinstein Feb 05 '22 at 19:55
  • In postman, where is the `API_KEY` set? And could you update your script with `JSON.stringify(data)` and `console.log(response.getResponseCode())` – TheMaster Feb 05 '22 at 20:18
  • @TheMaster Updated per our comment. In Postman, the key is after Bearer in the Authorization. Toward the bottom of the screenshot. – DBWeinstein Feb 05 '22 at 20:58
  • So try removing it from the url, `?api_key=xxxxx` in the url and send it as header here in urlfetch too. contentType syntax is wrong as well. `var options = { "method" : "post", contentType: 'application/json', "payload" : JSON.stringify(data), headers: {Authorization: \`Bearer xxxAPIKEYxxx\`} };` Also try changing `"1234"` to `1234`, if the rest doesn't work. – TheMaster Feb 05 '22 at 21:59

1 Answers1

0

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

DBWeinstein
  • 8,605
  • 31
  • 73
  • 118