Language Reference Guide : System Classes : UrlConnection Class : UrlConnection Class Examples
 
Share this page          
UrlConnection Class Examples
Example 1: Get the HTML from an HTTP URL into a StringObject
DECLARE
    urlc = UrlConnection;
    rv = INTEGER NOT NULL;
    so = StringObject;
    txt = VARCHAR(2000) NOT NULL;
    i = INTEGER NOT NULL;
ENDDECLARE
{
    rv = urlc.SetOption(option = CURLOPT_URL, value = 'http://www.example.com');
    rv = 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
        rv = urlc.GetInfo(info = CURLINFO_EFFECTIVE_URL, value = BYREF(txt));
        IF rv = ER_OK THEN
            MESSAGE 'EFFECTIVE_URL: ' + txt;
        ENDIF;
        rv = urlc.GetInfo(info = CURLINFO_CONTENT_TYPE, value = BYREF(txt));
        IF rv = ER_OK THEN
            MESSAGE 'CONTENT_TYPE: ' + txt;
        ENDIF;
    ELSE
        CurExec.Trace(text = urlc.Errortext);
    ENDIF;
    ENDIF;
    
}
Example 2: Copy a file from a server using SCP into local file
DECLARE
    urlc = UrlConnection;
    rv = INTEGER NOT NULL;
    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
        MESSAGE urlc.Errortext;
    ENDIF;
}
Example 3: Upload a file to a server using SFTP
DECLARE
    urlc = UrlConnection;
    rv = INTEGER NOT NULL;
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
        rv = 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;
    rv = INTEGER NOT NULL;
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;
    rv = INTEGER NOT NULL;
ENDDECLARE
{
    urlc.SetOption(option = CURLOPT_URL,
        value = 'http://myserver:8080/openroad/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;
}
The same could be achieved 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/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;
}