KM Variable Causing Trouble with Airtable API

Not sure if this an issue with Airtable's API, my script or a bit of both. I'm trying to send KM variables to Airtable via their API. Here's what I'm using:

curl -v -X PATCH https://api.airtable.com/v0/xxxxxxxxxx/Students%2C%20Parents%2C%20Prospectives \
          -H "Authorization: Bearer xxxxxxxxxx" \
          -H "Content-Type: application/json" \
          --data '{
         "records": [
    {
      "id": "'$KMVAR_ATRecordID'",
      "fields": {
        "Status": "'$KMVAR_Status'",
	"Lesson Day": [ "'$KMVAR_Lesson_Day'" ],
	"Instrument": [ "'$KMVAR_Instrument'" ],
	"Teacher": [ "'$KMVAR_Teacher'" ] 
      }
    }
  ]
}'

This ALMOST works, but fails with "Teacher": [ "'$KMVAR_Teacher'" ]. The log shows an unmatched unmatched close brace/bracket error but I can NOT solve it! The odd part is that If I remove that line from the script (so that $KMVAR_Instrument is the last bit), it works perfectly.

Similarly, if I run the script from Terminal, it works:

curl -v -X PATCH https://api.airtable.com/v0/xxxxxxxxxx/Students%2C%20Parents%2C%20Prospectives
-H "Authorization: Bearer xxxxxxxxxx"
-H "Content-Type: application/json"
--data '{
"records": [
{
"id": "rec9IIGSH47WWcs4t",
"fields": {
"Status": "active",
"Lesson Day": [ "Monday" ],
"Instrument": [ "bass" ],
"Teacher": [ "Bob Smith" ]
}
}
]
}'

Any thoughts?

Wild guess that I can't test, but maybe you need a comma at the end of the teacher line? When you say it works when you remove that line, are you leaving the comma on the Instrument line?

Hey @brijazz,

You didn't post your macro, so no one has any idea of what you're really doing. Therefore any advice given is mostly conjecture.

Please post the minimum possible “working” version of the macro.

The devil is in the details...

If you haven't read these they're worth a couple of minutes of your time.

How to Post Your Macro to the Forum

Tip: How Do I Get The Best Answer in the Shortest Time?

-Chris

Nope! The comma has to be removed from the Instrument line for the script to work.

Thanks, @ccstone. I'm not sure what the 'minimum possible' version is, but here's the macro. I'm using KM to ask for a customer's name, then getting that customer's matching Airtable record ID, entering which info I'd like to change and pushing that to the curl script. Everything works until the final 'Execute Shell Script'.

Keyboard Maestro Actions.kmactions (11 KB)

1 Like

My advice is to never, ever compose a shell script from components inside an Execute a Shell Script action – at least not until you know you have it working correctly.

Build it in a variable.

How to Build a Shell Script for Later Execution v1.00.kmmacros (6.9 KB)

I'm not a huge fan of using eval, but it works if you know what you're doing.

A better plan is to write the composed command string to a file and then run the file from the Execute a Shell Script action.

That way you can turn OFF the execute action and open the file in BBEdit for a careful look-see. You can also use BBEdit's compare file feature (commercial OR freeware version) to compare the newly composed shell script with what you know works in the Terminal.

** Note the single quotes in the DEBUG action – they're there because the action trims whitespace without telling you, and they prevent that. Hopefully Keyboard Maestro 10 will have the ability to turn that OFF without using a workaround.

HTH.

-Chris

Thanks for all of this Chris, I really appreciate the time and effort :+1:t2:

I believe I understand the concept, but am not familiar with eval (I did read up a bit on it though!) and don't know how to go about writing the command to a file as proposed. Here's where I stand at the moment:

Running this prompts a notification from KM saying that the script has failed.

NB: the disabled action is the same as the one picture below it albeit with my personally-identifiable Airtable info in it. I'm of course running that one when I test (i.e. I'm not sending a bunch of xxx where my API token should be!).

Examine how I've done this one.

Write a Shell Script to a File and Execute the File v1.00.kmmacros (8.3 KB)

From here you have a file on disk.

If the shell script you assemble doesn't work, you can compare it with BBEdit's compare function to the version that does work in the Terminal and see what the differences are.

Debugging macros is not always an easy job...

-Chris

Maybe I'm misunderstanding something. I was thinking that the goal was to have KM output a file that shows the entire script that it would be executing, with the values of the variables in place of the names of the variables - eg. the script file would read "Teacher: James Joyce" instead of "Teacher": [ "'$KMVAR_Teacher'" ] Am I mistaken in expecting this output?

I've built my command in my existing action, and copy-pasted it into the Set Variable “local_TestVar” to Text action in your macro - it looks like this:

When I run it I get:

I've tried running a few things straight from inside Terminal as well. This works:

curl -v -X PATCH https://api.airtable.com/v0/xxxxxxxx/Students%2C%20Parents%2C%20Prospectives \
  -H "Authorization: Bearer xxxxxxxx" \
  -H "Content-Type: application/json" \
  --data '{
  "records": [
    {
      "id": "rec9IIGSH47WWcs4t",
      "fields": {
	"Instrument": [ "piano" ]
      }
    }
  ]
}'

This does not (with Airtable's API complaining that I'm not providing an array with a Record ID or Field Object):

curl -v -X PATCH https://api.airtable.com/v0/xxxxxxxx/Students%2C%20Parents%2C%20Prospectives \
  -H "Authorization: Bearer xxxxxxxx" \
  -H "Content-Type: application/json" \
  --data '{
  "records": [
    {
      "id": "'$KMVAR_ATRecordID'",
      "fields": {
	"Instrument": [ "'$KMVAR_Instrument'" ]
      }
    }
  ]
}'

As an (possibly related?) aside: when I try to echo my variables in Terminal (i.e. via echo $KMVAR_Instrument or $KMVAR_Lesson_Day) a blank line is returned even though those variables have values in KM.

I had a lightbulb moment: the only variable that was causing me problems was $KMVAR_Teacher... and it's also the only variable that a space in it (i.e. "James Joyce"). I quoted it (so that there's a double-quote inside a single-quote inside a double-quote!) and all is well.

The successful Execute Shell Script action looks like this:

Good deal.