Was this helpful?
UrlConnection Class Examples
Example 1: Get the HTML from an HTTP URL into a StringObject
DECLARE
    urlc = UrlConnection;
    so = StringObject;
    txt = VARCHAR(2000) NOT NULL;
ENDDECLARE
{
    urlc.SetOption(option = CURLOPT_URL, value = 'http://www.example.com');
    urlc.SetOption(option = CURLOPT_FOLLOWLOCATION, value = TRUE);
    urlc.OutputObject = so;
    // Default options will perform a GET on a HTTP URL
    IF urlc.Perform()=ER_OK THEN
        MESSAGE 'HTTP GET was OK - so.Length: ' + varchar(so.Length);
        // Now get some information about the transferred URL
        IF urlc.GetInfo(info = CURLINFO_EFFECTIVE_URL, value = BYREF(txt)) = ER_OK THEN
            MESSAGE 'EFFECTIVE_URL: ' + txt;
        ENDIF;
        IF urlc.GetInfo(info = CURLINFO_CONTENT_TYPE, value = BYREF(txt)) = ER_OK THEN
            MESSAGE 'CONTENT_TYPE: ' + txt;
        ENDIF;
    ELSE
        CurExec.Trace(text = urlc.Errortext);
    ENDIF;
    
}
Example 2: Copy a file from a server using SCP into local file
DECLARE
    urlc = UrlConnection;
    so = StringObject;
ENDDECLARE
{
    urlc.SetOption(option = CURLOPT_URL, value = 'scp://myserver//tmp/file1.txt');
    urlc.SetOption(option = CURLOPT_USERPWD, value = 'myuser:mypassword');    
    urlc.OutputFile = 'c:\temp\file1.txt';
    IF urlc.Perform() = ER_OK THEN
        CurExec.Trace(text = 'File successfully copied from server.');
    ELSE
        CurExec.Trace(text = urlc.Errortext);
    ENDIF;
}
Example 3: Upload a file to a server using SFTP
DECLARE
    urlc = UrlConnection;
ENDDECLARE
{
    urlc.SetOption(option = CURLOPT_URL, value = 'sftp://myserver//tmp/f2.txt');
    urlc.SetOption(option = CURLOPT_USERPWD, value = 'myuser:mypassword');
    urlc.SetOption(option = CURLOPT_UPLOAD, value = 1);
    urlc.InputFile = 'c:\temp\f2.txt';
    IF urlc.Perform() = ER_OK THEN
        CurExec.Trace(text = 'File uploaded.');
    ELSE
        CurExec.Trace(text = urlc.Errortext);
    ENDIF;
}
Example 4: Upload a file in ASCII mode to a server using FTP
Use the name “upld.txt” during transfer and rename the file after the upload.
Note:  FTP protocol commands (rather than FTP client commands) must be used for the “QUOTE,” “PREQUOTE,” or “POSTQUOTE” options.
DECLARE
    urlc = UrlConnection;
    pqa = ARRAY OF StringObject;
    i8val = INTEGER8 NOT NULL;
ENDDECLARE
{
    urlc.SetOption(option = CURLOPT_URL, value = 'ftp://myservrr//tmp/upld.txt');
    urlc.SetOption(option = CURLOPT_USERPWD, value = 'myuser:mypassword');
    urlc.SetOption(option = CURLOPT_UPLOAD, value = 1);
    urlc.SetOption(option = CURLOPT_TRANSFERTEXT, value = 1); // ASCII transfer
    // Rename file after upload
    pqa[1].Value = 'RNFR upld.txt';
    pqa[2].Value = 'RNTO myfilea.txt';
    urlc.SetOption(option = CURLOPT_POSTQUOTE, value = pqa);
    urlc.InputFile = 'c:\temp\my.txt'; // Local file to upload
    IF urlc.Perform() = ER_OK THEN
        urlc.GetInfo(info = CURLINFO_SPEED_UPLOAD_T, value=BYREF(i8val));
        CurExec.Trace(text = 'File uploaded - '+ varchar(i8val) +' bytes/sec.');
    ELSE
        MESSAGE urlc.Errortext;
    ENDIF;
}
Example 5: Send email using SMTP
DECLARE
    urlc = UrlConnection;
    str = StringObject;
ENDDECLARE
{
    str.Value = '{"URL":"smtp://smtp.actian.com","UPLOAD":1,'+
        '"MAIL_FROM":"<john.doe @actian.com>",'+
        '"MAIL_RCPT":{"rows":[{"stringvalue":"<whoever@actian.com>"}]}}';
    urlc.SetOptions(options = CurSession.JsonHandler.Parse(json = str));
    str.Value = 'From: John Doe <john.doe@actian.com>'+ HC_NEWLINE +
        'To: Whoever <whoever@actian.com>'+ HC_NEWLINE +
        'Subject: Test email from OpenROAD using UrlConnection class'+
        HC_NEWLINE + HC_NEWLINE + 'Hello,'+ HC_NEWLINE + 'this is a test.';
    urlc.InputObject = str;
    IF urlc.Perform() = ER_OK THEN
        CurExec.Trace(text = 'Email sent.');
    ELSE
        CurExec.Trace(text = urlc.Errortext);
    ENDIF;
}
Example 6: Do a REST API update using an HTTP PUT request and write the response into StringObject
DECLARE
    urlc = UrlConnection;
ENDDECLARE
{
    urlc.SetOption(option = CURLOPT_URL,
        value = 'http://myserver:8080/myapi/123');
    urlc.SetOption(option = CURLOPT_POSTFIELDS,
        value = '{"name":"foo","price":4.56}');
    urlc.SetOption(option = CURLOPT_CUSTOMREQUEST, value = 'PUT');
    urlc.OutputObject = StringObject.Create();
    IF urlc.Perform() = ER_OK THEN
        CurExec.Trace(string = urlc.OutputObject);
    ELSE
        CurExec.Trace(text = urlc.Errortext);
    ENDIF;
}
Example 7: Execute a JSON-RPC request as an HTTP POST request
DECLARE
    urlc = UrlConnection;
ENDDECLARE
{
    urlc.SetOption(option = CURLOPT_URL,
        value = 'http://myserver:8080/openroad/jsonrpc?app=jsonrpcservertest');
    urlc.SetOption(option = CURLOPT_POSTFIELDS,
        value = '{"jsonrpc":"2.0","id":1,"method":"subtract",'+
            '"params":{"subtrahend":23.4,"minuend":42.8}}');
    urlc.OutputObject = StringObject.Create();
    IF urlc.Perform() = ER_OK THEN
        CurExec.Trace(string = urlc.OutputObject);
    ELSE
        CurExec.Trace(text = urlc.Errortext);
    ENDIF;
}
You could achieve the same using a RemoteServer.JsonRpcRequest() method invocation:
DECLARE
    resp = StringObject DEFAULT NULL;
ENDDECLARE
{
    CurRemoteServer.Initiate(image ='', type = RP_SHARED, routing='http-jsonrpc',
        location='http://myserver:8080/openroad/jsonrpc?app=jsonrpcservertest');
    resp = CurRemoteServer.JsonRpcRequest(request=ToString(text =
        '{"jsonrpc":"2.0","id":1,"method":"subtract",'+
        '"params":{"subtrahend":23.4,"minuend":42.8}}'));
    IF resp IS NOT NULL THEN
        CurExec.Trace(string = resp);
    ELSE
        CurExec.Trace(text = CurRemoteServer.Errortext);
    ENDIF;
}
Example 8: Use a SOAP service (NumberToWords) to convert an integer into word(s) corresponding to the number passed as parameter
procedure Number2Words(
    num = integer not null default 1234
)=
declare
    urlc = UrlConnection;
    soaprequest = VARCHAR(2000) NOT NULL;
    result = VARCHAR(16000) NOT NULL;
    hdrs = ARRAY OF StringObject;
    str = StringObject;
    xd = XmlDocument;
enddeclare
{
    // See https://www.dataaccess.com/webservicesserver/numberconversion.wso?op=NumberToWords
    // Note: SOAP service only accepts unsigned integer, therefore handling of negative values is done in 4GL
    soaprequest = '<?xml version="1.0" encoding="utf-8"?>' +
        '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>' +
                '<NumberToWords xmlns="http://www.dataaccess.com/webservicesserver/">' +
                  '<ubiNum>' + VARCHAR(ABS(num)) + '</ubiNum>' +
                '</NumberToWords>' +
        '</soap:Body></soap:Envelope>';
 
    hdrs[1].Value = 'Content-Type: text/xml; charset=utf-8';
    urlc.SetOption(option = CURLOPT_HTTPHEADER, value = hdrs);
    urlc.SetOption(option = CURLOPT_URL, value = 'https://www.dataaccess.com/webservicesserver/NumberConversion.wso');   
    urlc.SetOption(option = CURLOPT_SSL_VERIFYPEER , value = FALSE);
    urlc.SetOption(option = CURLOPT_POSTFIELDS, value = soaprequest);
    urlc.OutputObject = StringObject.Create();
    IF urlc.Perform() = ER_OK THEN
        xd.ParseString(string = urlc.OutputObject);
        //  Get text of <soap:Envelope><soap:Body><m:NumberToWordsResponse><m:NumberToWordsResult> element
        XmlElement(XmlElement(XmlElement(xd.RootElement.Children[1]).Children[1]).Children[1]).GetCharacterValue(string = BYREF(str));
        IF num<0 THEN
            result = 'minus ' + str.Value;
        ELSE
            result = str.Value;
        ENDIF;
    ELSE
        CurExec.Trace(text = urlc.Errortext);
    ENDIF;
    RETURN result;
}
Last modified date: 06/25/2024