« XDC 2014 just 6 month… | Home | Conference and Traini… »

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 )
MBS( "CURL.SetOptionCustomRequest"; handle; value )

and in Xojo & Real Studio:

curl.OptionPost = true
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.
26 09 13 - 20:51
two comments

Hello: I am trying to use CURL to add an attachment to an issue in Jira. Their API shows this call: “curl D -u admin:admin -X POST -H “X-Atlassian-Token: no-check” -F “file=@myfile.txt” “http://myhost/rest/api/2/issue/TEST-123/attachments”“:http://myhost/rest/api/2/issue/TEST-123/attachments” but I can’t get this to work using my account info.

How do I call this from your plug-in?

Laura Bowyer - 25 01 16 - 23:21

This should work.

CURL.SetOptionUserName with “admin”
CURL.SetOptionPassword with “admin”
CURL.SetOptionPost with 1
CURL.SetOptionHTTPHeaders with “X-Atlassian-Token: no-check”
CURL.SetOptionURL with “http://myhost/rest/api/2/issue/TEST-123/attachments”

And for the file attachment, you can use CURL.FormAddKeyFile. Or if that file contains just text use CURL.FormAddKeyText.
Christian Schmitz - 25 01 16 - 23:49

Remember personal info?

Emoticons / Textile

Hide email:

Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.