Help

Create Comment Via Script

Topic Labels: Automations
Solved
Jump to Solution
549 2
cancel
Showing results for 
Search instead for 
Did you mean: 
Devon_Godfrey
6 - Interface Innovator
6 - Interface Innovator

I'm going based on this: https://airtable.com/developers/web/api/create-comment

The authorization is fine, but for some reason within scripting it keeps giving the error that the body isn't parsable. My script is as follows: 

 

 

 

//Variables
let inputConfig = input.config();
let recordID = inputConfig.recordID;
let commentContent = "Hello World";

let url = 'https://api.airtable.com/v0/appksePpf16d6MPdp/tbl4ZEftsWgo84Ldu/' + recordID + '/comments';
console.log(commentContent);

//Create the Comment
let response = await fetch(url, {
method: 'POST',
headers: {
'Authorization': 'Bearer [private]',
'Content-Type': 'application/json',
},
body: { //For some reason, it's not accepting this body
'text': commentContent
}
});
console.log(await response.text());
//console.log(await response.json());

 

 

 

I've gotten it to work with this exact same data in Spectator, so I'm really puzzled as to why this doesn't work. 

1 Solution

Accepted Solutions
TheTimeSavingCo
18 - Pluto
18 - Pluto

Hmm, try doing a JSON.stringify on commentContent?

let bodyContent = JSON.stringify({
    'text': commentContent
  });

  let response = await fetch(url, {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer [private]',
      'Content-Type': 'application/json',
    },
    body: bodyContent // Pass the stringified JSON as the body
  });

See Solution in Thread

2 Replies 2
TheTimeSavingCo
18 - Pluto
18 - Pluto

Hmm, try doing a JSON.stringify on commentContent?

let bodyContent = JSON.stringify({
    'text': commentContent
  });

  let response = await fetch(url, {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer [private]',
      'Content-Type': 'application/json',
    },
    body: bodyContent // Pass the stringified JSON as the body
  });

Whelp, that did it. Thank you Adam! 

I took what I made and converted it into a script for anyone else looking to do this. You can find a pastebin for it here, but here it is pasted below as well: 

 

 

 

/*******
Instructions
1. Configure the variables below under "Configuration"
2. Set up your inputs. You can modify the configComment however you please but make configRecord just drop in a string for the record ID.
3. Test it to see if it works. If it isn't the console outputs the error for you.
*******/
 
let inputConfig = input.config();
//Configuration
    let configBase = "app---";// You can retrieve these values from the URL of any open table
    let configTable = "tbl---";
    let configRecord = inputConfig.recordID;//Pass a value into here from input variables
    let configComment = "Hello there! This is a comment!";//Modify however you want. 
    let configBearerToken = "---";//To generate this, look here: https://airtable.com/create/tokens
 
//Variables
    let bodyContent = JSON.stringify({
        'text': configComment
    });
    let url = 'https://api.airtable.com/v0/' + configBase + '/' + configTable + '/' + configRecord + '/comments';
 
//Create the Comment
    let response = await fetch(url, {
        method: 'POST',
        headers: {
            'Authorization': 'Bearer ' + configBearerToken,
            'Content-Type': 'application/json',
        },
        body: bodyContent
    });
    console.log(await response.text());