Portage Bay
Solutions
Portage Bay

FileMaker 16 and cURL

good_curl_logo.png

One of the more exciting new features of FileMaker 16 is the adoption of the cURL library within the “Insert from URL” script step. Using cURL as a command-line tool is relatively straightforward, but the syntax can often be a hurdle. While exploring the FileMaker forums and 3rd party blog posts, I noticed a majority of new FileMaker users seem to have questions with command syntax for FileMaker’s implementation of the cURL library, so I’ve created a sample file with some simple examples to highlight some common use scenarios. Feel free to download the sample file below and use it as a start for implementing cURL into your own solutions.

Upload File to FTP

In the example script “File_Upload”, we’re uploading a file stored in a FileMaker container to an FTP site (ftp://speedtest.tele2.net/upload/). It’s important to note that FileMaker requires that the file being sent be stored in a variable. That variable is then referenced in the cURL options

#Set the file to a variable. FM's Insert from URL requires this method to send files
Set Variable [ $post_data ; Value: Content Management::File Container ]

#Set curl options: upload file, timeout(10), trap for curl error Set Variable [ $curloptions ; Value: "-T $post_data --connect-timeout 10 --show-error" ]

Using the “Set Error Capture” to “On” in this script will only capture any FM-native, non-curl errors. Specifying the “--show-error” in the cURL option will capture any cURL-specific errors returned by the server. We’re capturing these errors with both the “Get ( LastError )” and “Get ( LastExternalErrorDetail )” script steps.

#Capture any non-curl FM errors
Set Variable [ $err ; Value: Get ( LastError ) ]

#Capture any curl-specific errors
Set Variable [ $curl_err ; Value: Get ( LastExternalErrorDetail ) ]

The FTP site we’re using for testing here is offered by Tele2, which is most commonly known for their speedtest.net service. This FTP is a free service that can be used to test both uploads and downloads.

Quick tangent: In this example, I’m using a modular dialog that I put together to show a simple confirmation message to the user. It’s a simple slider panel that sits on the top-right of the layout that, when called, sets a global variable, then slides to the visible tab object to show the message. In my next blog post, I’ll be discussing this module in more detail, including where to get it and how to install it into your own solutions.

 

Validate URL

To validate a URL using cURL, in the script “Validate_URL”, we’re testing for a valid response from any website. When a URL is not valid, most browsers typically respond with a “URL search”, so we’re parsing the response for a “FailedURI”. The lack of that pattern means the url returned the correct header.

#parse for failed URI
If [ PatternCount ( Content Management::g_text ; "FailedURI" ) > 0
Set Variable [ $err ; Value: "Failed URI" ]
End If

 

List Directory

The site we used to test the file upload also hosts a number of files to test download speeds. We can use that same URL to get a list of the files in that directory. Since we just want the list of files, we’re specifying the  “--list-only” option (-l) and dumping the result directly into a global field in the script “List_Dir”.

#use the "-l" option to only list filenames
Set Variable [ $curloptions ; Value: /"-u " & $user&":"&$pswd & /"-l --show-error" ]
Insert from URL [ Verify SSL Certificates ; Select ; With dialog: Off ; Content Management::g_text ; $url ; cURL options: $curloptions ]

 

HTTPS POST

In the script “HTTPS_POST”, we’re using the example included in the FileMaker 16 curl manual, but doctoring it up for real-world use. In this instance, the response is not necessary. The important bits are in the header.

Set Variable [ $curloptions ; Value: "-X POST " & "-D $header " & "--user myusername:mypassword " & "--data fname=" & Content Management::Name & "lname=" & GetContainerAttribute ( Content Management::File Container ; "filename" ) ]
Insert from URL [ Verify SSL Certificates ; Select ; With dialog: Off ; $response ; $url ; cURL options: $curloptions ]

In the above example, it’s worth noting that you can now set the result as a variable in FileMaker 16. In prior versions of FileMaker, the Insert from URL script step required a target field to be placed on the layout to set the result.

For more examples, Mike Duncan at Soliant did an excellent post on cURL and Container Fields, Eric Church at DB Services goes into detail on the FM cURL options, and the FileMaker Community has an excellent post by “mrwatson-gbs” who goes into much detail on using cURL in FM.