Sv: O7: using cURL

Daniel Sananes daniel.s at kopparbergs.se
Tue Jan 5 14:35:15 UTC 2021


Thanks Rudolf and Phil.

The command Start program normal {[L_FILE_TO_CURL]}     ;; This works
L_FILE_TO_CURL is a local variable holding the name of the bat-file containing the request.
A simple textfile with the file extension .bat
curl -H "etc etc etc" and it ends with ">> fromcURL.txt" which gives the cURL answer in a textfile that Omnis can read.

Regards
Daniel


-----Ursprungligt meddelande-----
Från: omnisdev-en <omnisdev-en-bounces at lists.omnis-dev.com> För Rudolf Bargholz
Skickat: den 5 januari 2021 10:24
Till: OmnisDev List - English <omnisdev-en at lists.omnis-dev.com>
Ämne: AW: O7: using cURL

Hi Daniel,

Below some notes I made for one of our interfaces and some code. We call curl, write the output to an outfile, and then wait for curl to write the file and release the lock on the file before continuing with the Omnis code workflow. The important thing to take into account is that it might take a short while before the outfile is created by curl, so we have a helper method that waits for a few seconds, or until the outfile has been created, then wait until the file is no longer locked by curl, and only then continue with the code.

Hope this helps.

Regards

Rudolf Bargholz

;  #FILETOSEND#
;  #LOGFILE#
;  #CERTIFICATEFILE#
;  #CERTIFICATEPASSWORD#

;  -X POST --data-binary @"#FILETOSEND#" --output "#LOGFILE#" --cert-type P12 --cert "#CERTIFICATEFILE#":#CERTIFICATEPASSWORD# --connect-timeout 5 --max-time 30 -H "Content-Type:application/xml" https://cmirtest.xxx.com/cmir/cmir.aspx

;  Launch program "C:\...\curl.exe", -X POST --data-binary @"C:\temp\data.xml" --output "C:\temp\cmir_test_tls\out.log" --cert-type P12 --cert " C:\temp cmirtest_onlinetravelport.ch_.p12":cVBicxxxxxxxxxx5oZ2I= --connect-timeout 5 --max-time 30 -H "Content-Type:application/xml" https://cmirtest.xxx.com/cmir/cmir.aspx Returns #S1 (Do not quit Omnis)


Calculate lCurlParams as replaceall(lCurlParams,'#FILETOSEND#',lCurlFileToSend)
Calculate lCurlParams as replaceall(lCurlParams,'#LOGFILE#',lCurlLogFile)
Calculate lCurlParams as replaceall(lCurlParams,'#CERTIFICATEFILE#',lCertificateFilePath)
Calculate lCurlParams as replaceall(lCurlParams,'#CERTIFICATEPASSWORD#',lCurlCertificatePassword)
Start program minimized {"[lCurlExePath]" [lCurlParams]} Do oFileHandling.$waitForResponseFile(lErrText,lCurlLogFile,1,30,30) Returns FlagOK If FlagOK If lFileOps.$openfile(lCurlLogFile) Do lFileOps.$readfile(lLogOutData) Do lFileOps.$closefile() Else Calculate lErrText as con('Error: Unable to open curl log file ',lCurlLogFile) Calculate FlagOK as kFalse End If Else Calculate lErrText as con('Error: Unable to find and open curl log file ',lCurlLogFile) End If

End If

If FileOps.$doesfileexist(lCurlLogFile)
Do FileOps.$deletefile(lCurlLogFile)
End If
Do lFileOps.$destruct()

If FlagOK
Calculate pfReturnData as lLogOutData
Else
Calculate pfReturnData as lErrText
End If

-----------------

Calculate lUrl as iWireCardUserRow.URL
Calculate lUsername as iWireCardUserRow.USER Calculate lPassword as iWireCardUserRow.PASSWORD Calculate lContentType as con('Content-Type:',pContentType) Calculate lAuthenticationBaseString as con(lUsername,':',lPassword) Calculate lAuthEncoded as base64Encode(lAuthenticationBaseString)

Launch program curl -S -X POST --header "[lContentType]" --header "Authorization: Basic [lAuthEncoded]" --data "@[lRequestFile]" "[lUrl]" -o "[lOutFile]" --stderr "[lErrorFile]" Returns #S1 (Do not quit Omnis)

-----------------

$waitForResponseFile

Calculate FlagOk as kTrue

If pTimeoutSecondsStartWait<1
Calculate pTimeoutSecondsStartWait as 1
End If
If pTimeoutSecondsFileCreateStartWait<5
Calculate pTimeoutSecondsFileCreateStartWait as 5 End If If pTimeoutSecondsProcessWait<2 Calculate pTimeoutSecondsProcessWait as 2 End If

Calculate timeoutTicks as 60*pTimeoutSecondsStartWait     ;; 60 ticks * seconds
Calculate ct as #CT
Repeat
Until abs(#CT-ct)>timeoutTicks

Calculate timeoutTicks as 60*pTimeoutSecondsFileCreateStartWait     ;; 60 ticks * seconds
Calculate timeoutFlag as kTrue
Calculate ct as #CT
Repeat
If FileOps.$doesfileexist(pFile)
Calculate timeoutFlag as kFalse
End If
Until abs(#CT-ct)>timeoutTicks|not(timeoutFlag)

If timeoutFlag
;  The process has timed out
Calculate FlagOk as kFalse
Calculate pfErrMsg as con('Unable to find file after timeout(',pTimeoutSecondsStartWait,'): ',pFile) Else
Calculate timeoutTicks as 60*pTimeoutSecondsProcessWait     ;; 60 ticks * seconds
Calculate timeoutFlag as kTrue
Calculate ct as #CT
Repeat
If lFileOpsObj.$openfile(pFile)
Do lFileOpsObj.$closefile()
Calculate timeoutFlag as kFalse
End If
Until abs(#CT-ct)>timeoutTicks|not(timeoutFlag)

If timeoutFlag
;  The process has timed out
Calculate FlagOk as kFalse
Calculate pfErrMsg as con('Unable to open file after timeout(',pTimeoutSecondsProcessWait,'): ',pFile) End If

End If

Quit method FlagOk

-----Ursprüngliche Nachricht-----
Von: omnisdev-en [mailto:omnisdev-en-bounces at lists.omnis-dev.com] Im Auftrag von Daniel Sananes
Gesendet: Dienstag, 5. Januar 2021 10:02
An: OmnisDev List - English <omnisdev-en at lists.omnis-dev.com>
Betreff: O7: using cURL

Hi
We have new projects that involves making API-calls.
I have familiarized myself with cURL to perform these calls.
But how would I use cURL inside Omnis7?
With the command Start program I can start the command prompt but how do I read the answer from the cURL-program?

I can open the command prompt and issue this command:
C:\Users\daniel.s>curl -H "Authorization: Basic Wkw5V0ExV1gxMzRBMzE4UktHOUowWFJXS0xaQVJTWUQ6TVNOSkpTRDVQTTlTRkMxQ1ZMREMyNllHVFVMVDRSWkU=" -X POST https://login.smartpooling.se/uaa/oauth/token -d grant_type=password -d username=Username -d password=Password

Which gives me this in return:
{"access_token":"51b6c672-3fd6-4bac-be9d-8ba19bfc03a6","token_type":"bearer","refresh_token":"fa6cd5f7-166b-479e-84a5-c38b0cd7c2ce","expires_in":31371,"scope":"read write"}

How would I issue the command from inside Omnis7 and how would I receive the returned text into Omis7?
Is this even possible?

Regards
Daniel Sananes
_____________________________________________________________
Manage your list subscriptions at http://lists.omnis-dev.com Start a new message -> mailto:omnisdev-en at lists.omnis-dev.com
_____________________________________________________________
Manage your list subscriptions at http://lists.omnis-dev.com Start a new message -> mailto:omnisdev-en at lists.omnis-dev.com 


More information about the omnisdev-en mailing list