Can anyone point me in the direct of good resources for using NSURLSession?
I’ve been trying to adapt the awesome work of @Piyomaru but am struggling.
I’m specifically trying to connect to Airtable which uses a bear token for authentication and while I’ve managed to connect via do shell I’m hope to see spead benefits of URLSession.
I’ve used URLSession in Swift in a SwiftUI app, and found Apple’s documentation, which includes sample code, useful. There’s also Stack Overflow posts with example code. But I haven’t used it with AppleScript or AppleScript Obje before. I’d also love to learn how though if it’s possible.
But I’m surprised and excited to hear it’s possible with do shell script and curl. I’ve used curl that way for a few things but had reluctantly kinda figured that it wouldn’t be sufficient to connect to a modern API.
Could someone please point me in the direction of any good resources for using curl through do shell script to connect with a web API?
Here’s a small example to use curl to talk to Filemaker thru it’s API…
on loadTokenForFmpDB()
– v20190626-LNT
set mUser to "username"
set mPasswd to "userPassword"
set mAuthentication to "Basic " & (my encodeBase64:(mUser & ":" & mPasswd))
set mCall to " -H \"Content-Type: application/json\" "
set mCall to mCall & " -H \"Content-Length: 0\" "
set mCall to mCall & " -H \"Authorization: " & mAuthentication & "\""
set mCurlCmd to "curl -X POST " & mCall & " "
-- set mShellCmd to (mCurlCmd & pFmpRESTurl & "auth/" & pFmpDB & " -d " & quoted form of mJson)
set mShellCmd to (mCurlCmd & " " & pFmpRESTurl & mFmpDB & "/sessions")
set jsonReply to do shell script mShellCmd
tell application "JSON Helper" to set json to read JSON from jsonReply
if json's messages's item 1's code is "212" then
return {err:-1, errMsg:json's messages's item 1's message}
end if
set mToken to json's response's token
return mToken
end loadTokenForFmpDB
You should look at the curl manual how to send a call with a header.
In this example from 2019 I still used the JSON Helper on newer version I replaced it with my own handler based on ASOC…
pFmpRESTurl, pFmpDB are defined at the top of the script library and refer as expected to the url and the name of the Filemaker database on the server.
It is a kind of mock up. I’m planing to write blog archive books. So, covers and other parts are already exists.
But I could not determine the language (Japanese or English).
So, header part contents (common parts) are already ready (in Japanese).
Here is a snippet that I’m using minus some senitive values.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
set apiKey to "Your API key"
set |Dept| to "A Variable you search for"
set theTask to "A view ID in airtable"
set ATable to "https://api.airtable.com/v0/appxXXXX"
set theCall to "curl '" & ATable & "?filterByFormula=(FIND(%22" & |Dept| & "%22%2C%7BTriggers%7D))&view=" & theTask & "' -H \"Authorization: Bearer " & apiKey & "\" -H \"Content-Type: application/json\""
tell current application to set theJsonCall to do shell script theCall
set theJsonTEXT to theJsonCall as text
set jsonString to current application's NSString's stringWithString:theJsonTEXT
set JSONdata to jsonString's dataUsingEncoding:(current application's NSUTF8StringEncoding)
set theJSON to (current application's NSJSONSerialization's JSONObjectWithData:((current application's
NSString's stringWithString:jsonString)'s dataUsingEncoding:(current application's
NSUTF8StringEncoding)) options:0 |error|:{})
set theCount to count of |records| of theJSON
This returns the Json response as a record that you can pretty simply get to the data you need. I find it faster than using JSON helper.