« Apple Tech Talks | Home | MBS Filemaker Plugin,… »

CURL from command line to Plugin

Sometimes people find example code for using some web service like this:

curl -k -H "APIKey : 3024224b-1322-41be-2323-12345678a530" -H "Content-Type: application/json" -X POST -d "[{SCID: 'TH258', DateWeekEnding:'2013-09-27',Details :[{PayElementCode : 'BASIC' , TotalValue:100.10}]}]" "https://mobile.something.test/Common.API/api/Payroll/PostPayslips"

Now how to translate to plugin calls?

Well, usually you can start with a sample which looks nearly like the one you need. Than you check option by option what it does and you add that option to your code.

So the first option is the -k option. This means we ignore security and do not verify SSL certificates. That's a bad setting as you can easily tell our plugin with OptionCAInfo about the file with certificates and you get it easily with Firefox. Now to disable security you would use this lines in FileMaker:

$r = MBS( "CURL.SetOptionSSLVerifyHost"; $curl; 0 )
$r = MBS( "CURL.SetOptionSSLVerifyPeer"; $curl; 0 )

and in Xojo & Real Studio:

curl.OptionSSLVerifyHost = 0
curl.OptionSSLVerifyPeer = 0

Next we have those header additions for HTTP Request. As you see there are two -H parameters, so we need to pass both options to the plugin. In both plugins we have a function for taking http headers and you pass an array there. In FileMaker it looks like this:

MBS( "CURL.SetOptionHTTPHeader"; $curl; "APIKey : 3024224b-1322-41be-2323-12345678a530"; "Content-Type: application/json" )

and in Xojo & Real Studio:

curl.SetOptionHTTPHeader array("APIKey : 3024224b-1322-41be-2323-12345678a530", "Content-Type: application/json")

The next option is -X POST which gives a custom request type with "POST" name. For that you can either use the post option or set a custom request which could be anything. So in FileMaker you use:

MBS( "CURL.SetOptionPost"; $curl; 1 )
or
MBS( "CURL.SetOptionCustomRequest"; handle; value )

and in Xojo & Real Studio:

curl.OptionPost = true
or
curl.OptionCustomRequest = "POST"

The -d parameter is the actual payload for the transfer. As this is a post, we can use the PostFields option. We simply pass the data as a big string like this in FileMaker:

MBS( "CURL.SetOptionPostFields"; $curl; "[{SCID: 'TH258', DateWeekEnding:'2013-09-27',Details :[{PayElementCode : 'BASIC' , TotalValue:100.10}]}]" )

and in Xojo & Real Studio:

curl.OptionPostFields = "[{SCID: 'TH258', DateWeekEnding:'2013-09-27',Details :[{PayElementCode : 'BASIC' , TotalValue:100.10}]}]"

The last option is the URL which we simply pass to the OptionURL and this works in FileMaker like this:

MBS( "CURL.SetOptionURL"; $curl; "https://mobile.something.test/Common.API/api/Payroll/PostPayslips" )

and in Xojo & Real Studio:

curl.OptionURL = "https://mobile.something.test/Common.API/api/Payroll/PostPayslips"

Of course to actually use curl, you need to use a few more commands like the perform command to actually start the transfer. And of course you need to prepare and cleanup the session. Not to mention that you should use OptionVerbose to get the debug messages about what happens. Claris FileMaker Plugin
26 09 13 - 20:51