Trouble Getting Variable into Curl Request to Airtable API with Shell Script action

Ok, so this may be complete ignorance on my part. but I'm trying to build a curl request via shell script to interact with the Airtable API. I can get it to work if I manually type the request in, but when I try to pass in one of my variables it all goes wonky. here's what my request looks like:

curl -v https://api.airtable.com/v0/appSvMPW2P7LjH0GU/High%20Schools \ -H "Authorization: Bearer xxxxxxxxxxxxx" \ -H "Content-type: application/json" \ -d "${KMVAR_VarName}" \

And Here's What I Get back:
` % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying 52.203.92.226...

  • TCP_NODELAY set
  • Connected to api.airtable.com (52.203.92.226) port 443 (#0)
  • TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
  • Server certificate: api.airtable.com
  • Server certificate: Amazon
  • Server certificate: Amazon Root CA 1
  • Server certificate: Starfield Services Root Certificate Authority - G2

POST /v0/appSvMPW2P7LjH0GU/High%20Schools HTTP/1.1
Host: api.airtable.com
User-Agent: curl/7.54.0
Accept: /
Authorization: Bearer xxxxxxxxxxxxxx
Content-type: application/json
Content-Length: 47

} [47 bytes data]

  • upload completely sent off: 47 out of 47 bytes
    < HTTP/1.1 422 Unprocessable Entity
    < Date: Sun, 22 Oct 2017 02:53:13 GMT
    < Content-Type: application/json; charset=utf-8
    < Content-Length: 82
    < Connection: keep-alive
    < Set-Cookie: AWSALB=wTViImgcEySIDmvzrgi02yvhuMwRmHuTrURnWQuye0K1ZBKg9KRes9VKOHXNJSBrZM2SOxwsjw/ehLh9bJKfBZUe0GPJGGaHPl8r0uWV/6OCboau7gf/hOcsrDBp; Expires=Sun, 29 Oct 2017 02:53:13 GMT; Path=/
    < Server: Tengine
    < access-control-allow-origin: *
    < access-control-allow-methods: GET,PUT,POST,PATCH,DELETE,OPTIONS
    < access-control-allow-headers: content-type, authorization, content-length, x-requested-with, x-api-version, x-airtable-application-id
    < ETag: W/"52-tRuy+2JsguCSmD5dT+0uwfeuSps"
    < Vary: Accept-Encoding
    <
    { [82 bytes data]

100 129 100 82 100 47 158 91 --:--:-{"error":{"type":"INVALID_REQUEST_BODY","message":"Could not parse request body"}}- --:--:-- --:--:-- 158

I have no idea what's going wrong.. I know that my JSON was good when I was using it directly

heres what I get back when I replace curl with echo: -v https://api.airtable.com/v0/appSvMPW2P7LjH0GU/High%20Schools -H Authorization: Bearer xxxxxxxxx -H Content-type: application/json -d '{ "fields": { "Name": "Westville" } }'

First, read this:

I presume the VarName variable is meant to have a JSON packet in it?

Where are the single quotes coming from? Because they are not in your curl line, and echo won't add them, so that makes me think they are in the VarName variable, which would not be valid JSON as far as I understand it.

I can nto troubleshoot in regards to the Airtable API, but it seems you write the VarName wrong in the shell script action.

There is missing a $ in front of the variable in you shell script.

This is the syntax.

$KMVAR_[variable_name]

See here: https://wiki.keyboardmaestro.com/action/Execute_a_Shell_Script

If you are referring to the "${KMVAR_VarName}”: This is a valid syntax and shouldn’t pose any problems in KM.

Ahh. Thanks Tom. Did not know that.

Hey guys, sorry for the noob question, but I am trying – and miserably failing – to pass KBM variables onto a curl request.

I’ve tried this:

curl -v -XPOST https://api.airtable.com/v0/XXXXX/Faturas \
-H "Authorization: Bearer XXXXXX" \
-H "Content-Type: application/json" \
--data '{
"fields": {
"Emissão": "${KMVAR_Emissao}",
"Valor": "${KMVAR_Valor}",
"Vencimento": "${KMVAR_Vencimento}",
"Pagamento": "Aberto"
}
}'

But only get this response:

{"error":{"type":"INVALID_VALUE_FOR_COLUMN","message":"Cannot parse date value \"${KMVAR_Emissao}\" for field Emissão"}}

I guess KBM is not replacing the variables on the curl command. How can I fix this?

Emissao is a yyyy-MM-dd formatted date, btw.

See the wiki article:

https://wiki.keyboardmaestro.com/action/Execute_a_Shell_Script#Quoting_Strings

Specifically the example emacsclient talks about how you cannot put variables within single quoted strings (as you have done for the multiline --data field). The article gives two solutions.

Thank you. Made it work. For anyone looking for a solution in the future.

  1. Open your AirTable
  2. Top Right Corner - Help - API Documentation
  3. On the Left find "Create records"

You will see a script for your AirTable that needs to be pasted into Keyboard Maestro Shell Window.

Whichever variable you create, in the script it has to be as '"$KMVAR_YourVariable"' then put it inside the " " of the AirTable curl script.

2 Likes