Programming Languages on Power

Power Programming Languages

IBM Power, including the AIX, IBM i, and Linux operating systems, support a wide range of programming languages, catering to both traditional enterprise applications and modern development needs.


#Power

 View Only
Expand all | Collapse all

REST call from IBMi to a MS SQL Server database

  • 1.  REST call from IBMi to a MS SQL Server database

    Posted 04/09/26 01:37 PM

    I'd like to make a REST call to a MS SQL Server database.  I googled the topic and it said to use the HTTP_GET_BLOB SQL Function. I've never done this before. I've made REST calls to a webservice running on the IBMi using Postman, but that's it. Is this the correct method?  Is there a place that gives some good examples other than this IBM page? 

    HTTP_GET and HTTP_GET_BLOB - IBM Documentation



    ------------------------------
    Michael Soucy
    Sr Midrange Engineer
    Bangor Savings Bank
    Bangor ME
    ------------------------------


  • 2.  RE: REST call from IBMi to a MS SQL Server database

    Posted 04/09/26 02:06 PM
    A while ago, I wrote a small blog post about using JSON_TABLE, which also uses QSYS2.HTTP_GET to retrieve the results of a web service call. 


    Maybe this helps a bit - there are also some good examples in the docs:


    I even have an example somewhere, which uses JSON_OBJECT to create the HTTP-options for the HTTP_GET call. 

    And FWIW - HTTP_GET_BLOB retrieves a binary large object from the server - like a JPEG or PDF file - if you receive JSON or XML you use HTTP_GET, HTTP_POST or whatever "verb" you need. 

    HTH
    Daniel






  • 3.  RE: REST call from IBMi to a MS SQL Server database

    Posted 04/10/26 07:40 AM

    I second Daniel's opinion. HTTP_GET is the way to go.  We've also used HTTPAPI, but this is a more modern method.


    Lori Austin

    Manager of  IT Solutions Engineering

    National Association of Letter Carriers

    202-662-2853 (office)







  • 4.  RE: REST call from IBMi to a MS SQL Server database

    Posted 04/10/26 08:56 AM

    Can you use HTTP_GET for something simple as downloading a file a webservice?  I had been working on a GoAnywhere project for something else I've gotten stuck. I wonder if the HTTP_GET would be easier.  I can download the files using Postman, but I would be better if I could do it directly on the IBMi.  This is what I put in for a GET operation in Postman and it worked.

    https://sanctionslistservice.ofac.treas.gov/api/download/{filename}

    {filename} is the OFAC file I need to download. 

    Would this be simpler using HTTP_GET?  I'd like the results to put into a file on the IFS.



    ------------------------------
    Michael Soucy
    Sr Midrange Engineer
    Bangor Savings Bank
    Bangor ME
    ------------------------------



  • 5.  RE: REST call from IBMi to a MS SQL Server database

    Posted 04/10/26 09:13 AM
    Edited by Paolo Salvatore 04/10/26 09:13 AM

    Hi,

    try open run sql script:
    and then:

    values http_get('https://sanctionslistservice.ofac.treas.gov/api/download/{filename}', '');




    ------------------------------
    Paolo Salvatore
    ------------------------------



  • 6.  RE: REST call from IBMi to a MS SQL Server database

    Posted 04/10/26 09:18 AM
    Of course Michael.

    You get the result as a CLOB (HTTP_GET) or BLOB (HTTP_GET_BLOB) - and then store it in an IFS file, or in a CLOB/BLOB field in a table.

    If you need more support please contact me.

    HTH
    Daniel




  • 7.  RE: REST call from IBMi to a MS SQL Server database

    Posted 04/10/26 09:28 AM

    Hi Daniel,

    I appreciate your offer to help me.  Using these HTTP calls is all new to me.  I seem to be struggling on how this works.  I was hoping to find some examples in the ACS SQL tool, but I didn't see any. 

    Thank you!



    ------------------------------
    Michael Soucy
    Sr Midrange Engineer
    Bangor Savings Bank
    Bangor ME
    ------------------------------



  • 8.  RE: REST call from IBMi to a MS SQL Server database

    Posted 04/10/26 10:12 AM
    What kind of file is that? Some sort of text/CSV, JSON or XML? Or something "binary"?

    You want the file to end up in the IfS? OK - there are several solutions for that. But the most straightforward would be, to declare a LOB-file variable in RPG and use it as the target of your HTTP_GET.

    The smallest example would be like this:

    dcl-s ifs_file sqltype(CLOB_FILE);

    ifs_file_fo = SQFOVR; // overwrite file contents
    ifs_file_name = '...your ifs path and file...';
    ifs_file_nl = %len(%trimr(ifs_file_name));

    exec sql set :ifs_file = http_get('...url...');
    if sqlcode <> *zero;
    // error
    endif;

    Please be advised, I'm on a road trip right now, and I don't have access to an IBM i - there might be errors and I might be wrong - that's what I recall from memory.

    HTH
    Daniel




  • 9.  RE: REST call from IBMi to a MS SQL Server database

    Posted 04/10/26 10:20 AM

    These are the 7 files I need to download and put in the Jack Henry IFS folder /JHA/JHAOFAC.  

    add.ff       
    alt.ff       
    cons_add.csv 
    cons_alt.csv 
    cons_prim.csv
    sdn.ff       
    sdn.xml      

    Thank you for your help!



    ------------------------------
    Michael Soucy
    Sr Midrange Engineer
    Bangor Savings Bank
    Bangor ME
    ------------------------------



  • 10.  RE: REST call from IBMi to a MS SQL Server database

    Posted 04/23/26 10:07 AM

    Hi Daniel Gross,

    I wrote an RPG program using your code.  When I try and run it, I get a sqlcode error -443. That seems to indicate that I need a certificate to download do the HTTP_GET function. How do I save the results to a file on the IFS after the HTTP_GET?

    Thank you!



    ------------------------------
    Michael Soucy
    Sr Midrange Engineer
    Bangor Savings Bank
    Bangor ME
    ------------------------------



  • 11.  RE: REST call from IBMi to a MS SQL Server database

    Posted 04/23/26 11:24 AM
    Hi Michael,

    well - the code should do that already. The variable IFS_FILE is declared and configured as a SQL-Type CLOB_FILE - which is automatically writing the contents that you set to the IFS file where the "sub-variable "_NAME" points. You can read about CLOB_FILE in the Info Center.

    The SQLCODE = -443 is a generic SQL code that signals an exception in a function or procedure. So you might have to diagnose the messages in the job log before that SQL0443 message - there should be some AXIS error messages from the C++ framework that does the work in the background.

    HTH
    Daniel




  • 12.  RE: REST call from IBMi to a MS SQL Server database

    Posted 04/23/26 03:27 PM

    Hi Daniel,

    I think I'm getting closer. The error is saying I need an SSL certificate.  I put the SSL cert into DCM following the instructions here:

    Configuring IBM i DB2 QSYS2 HTTP Functions for TLS/HTTPS Secure Communications

    When I run this RPG code it creates a file on the IFS, but it's empty.  Could our Firewall be blocking me from connecting to ofac.treas.gov?

    ctl-opt dftactgrp(*no);                                                     
                                                                                
    dcl-s ifs_file sqltype(CLOB_FILE);                                          
    dcl-c url 'https://sanctionslistservice.ofac.treas.gov/api/download/-       
    cons_add.csv';                                                              
                                                                                
    ifs_file_fo = SQFOVR; // overwrite file contents                            
    ifs_file_name = '/JHA/JHAOFAC/cons_add.csv';                            
    ifs_file_nl = %len(%trimr(ifs_file_name));                                  
                                                                                
    exec sql set :ifs_file =                                                    
    http_get(:url,'{"sslTolerate":"true"}');                                    
                                                                                
    if sqlcode <> *zero;                                                        
     // error                                                                   
    endif;                                                                      
                                                                                
    *inlr = *on;                                                                

    Thank you!



    ------------------------------
    Michael Soucy
    Sr Midrange Engineer
    Bangor Savings Bank
    Bangor ME
    ------------------------------



  • 13.  RE: REST call from IBMi to a MS SQL Server database

    Posted 04/23/26 04:08 PM
    Can it be, that your URL constant is missing the protocol?

    Shouldn't it be like this?
    dcl-c url 'https://sanctionslistservice.ofac.treas.gov/api/download/cons_add.csv';

    And have you checked the IFS permissions of the file and directory?

    HTH
    Daniel





  • 14.  RE: REST call from IBMi to a MS SQL Server database

    Posted 04/29/26 12:54 PM

    Hi Daniel,

    I ended up opening a case with IBM.  We were able to figure out what the issues were by looking at some logs. The items I was missing was I had to add "redirect":"5" to the call parameters and that did it. I updated the RPG code you provided and was able to prove that I could download all 7 files to the IFS.  This is my RPG code.  I'm going to clean it up, and change the IFS folder to where it actually needs to go to for the Jack Henry OFAC process.  Thank you for your help with this!

    **FREE
           ctl-opt dftactgrp(*no);

           dcl-s ifs_file sqltype(CLOB_FILE);

           ifs_file_fo = SQFOVR; // overwrite file contents
           ifs_file_name = '/SOUCYMI/JHAOFAC/sdn.xml';
           ifs_file_nl = %len(%trimr(ifs_file_name));

           exec sql set :ifs_file =
             http_get('https://sanctionslistservice.ofac.treas.gov/api/download/sdn.xml',
                 '{"sslTolerate":"true", "redirect":"5"}');

           if sqlcode <> *zero;
            // error
           endif;

           ifs_file_name = '/SOUCYMI/JHAOFAC/cons_add.csv';
           ifs_file_nl = %len(%trimr(ifs_file_name));
           exec sql set :ifs_file =
             http_get('https://sanctionslistservice.ofac.treas.gov/api/download/cons_add.csv',
                 '{"sslTolerate":"true", "redirect":"5"}');

           if sqlcode <> *zero;
            // error
           endif;

           ifs_file_name = '/SOUCYMI/JHAOFAC/cons_alt.csv';
           ifs_file_nl = %len(%trimr(ifs_file_name));
           exec sql set :ifs_file =
             http_get('https://sanctionslistservice.ofac.treas.gov/api/download/cons_alt.csv',
                 '{"sslTolerate":"true", "redirect":"5"}');

           if sqlcode <> *zero;
            // error
           endif;

           ifs_file_name = '/SOUCYMI/JHAOFAC/cons_prim.csv';
           ifs_file_nl = %len(%trimr(ifs_file_name));
           exec sql set :ifs_file =
             http_get('https://sanctionslistservice.ofac.treas.gov/api/download/cons_prim.csv',
                 '{"sslTolerate":"true", "redirect":"5"}');

           if sqlcode <> *zero;
            // error
           endif;
           *inlr = *on;

           ifs_file_name = '/SOUCYMI/JHAOFAC/alt.ff';
           ifs_file_nl = %len(%trimr(ifs_file_name));
           exec sql set :ifs_file =
             http_get('https://sanctionslistservice.ofac.treas.gov/api/download/alt.ff',
                 '{"sslTolerate":"true", "redirect":"5"}');

           if sqlcode <> *zero;
            // error
           endif;
           *inlr = *on;

           ifs_file_name = '/SOUCYMI/JHAOFAC/add.ff';
           ifs_file_nl = %len(%trimr(ifs_file_name));
           exec sql set :ifs_file =
             http_get('https://sanctionslistservice.ofac.treas.gov/api/download/add.ff',
                 '{"sslTolerate":"true", "redirect":"5"}');

           if sqlcode <> *zero;
            // error
           endif;
           *inlr = *on;

           ifs_file_name = '/SOUCYMI/JHAOFAC/sdn.ff';
           ifs_file_nl = %len(%trimr(ifs_file_name));
           exec sql set :ifs_file =
             http_get('https://sanctionslistservice.ofac.treas.gov/api/download/sdn.ff',
                 '{"sslTolerate":"true", "redirect":"5"}');

           if sqlcode <> *zero;
            // error
           endif;
           *inlr = *on;



    ------------------------------
    Michael Soucy
    Sr Midrange Engineer
    Bangor Savings Bank
    Bangor ME
    ------------------------------



  • 15.  RE: REST call from IBMi to a MS SQL Server database

    Posted 04/29/26 01:22 PM
    Perfect - glad it works - and yes the HTTP options are some kind of "alchemy" for us RPG developers ...

    I will make some blog post about it - thanks for your feedback.

    Regards,
    Daniel




  • 16.  RE: REST call from IBMi to a MS SQL Server database

    Posted 04/30/26 02:44 PM

    Hi Daniel,

    One more thing I wanted to share with you regarding this process.  When went to run the Jack Henry process that uses these files it failed.  It said it could not read the file. What I discovered was that the character set ID was 1208.  I checked the character set of what the files were when I downloaded them manually via Postmam they were 437.  I created a sub procedure in the RPG program to change the character set ID and that fixed the problem.  

    Here's my code:

           // --------------------------------------------------
           // Procedure name: ChangeCharSet
           // Purpose:        Change the IFS file to the correct character set
           // Returns:
           // Parameter:      p1_ifs_file_name
           // --------------------------------------------------
           dcl-proc ChangeCharSet;
             dcl-pi *N;
               p1_ifs_file_name CHAR(255);
             end-pi;

             // Local fields
             dcl-c CHGATR 'CHGATR OBJ(''%IFS_FILE%'') ATR(*CCSID) VALUE(437)';

             // Calculation specifications
             CmdString = CHGATR;
             CmdString = %scanrpl('%IFS_FILE%':%trim(p1_ifs_file_name):CmdString);

             Monitor;
               Command(%trimr(CmdString):%len(%trimr(CmdString)));
             on-error;
             endmon;

             return;
           end-proc;        

    Thank you again for all your help with this.  It has been a great learning experience for me.



    ------------------------------
    Michael Soucy
    Sr Midrange Engineer
    Bangor Savings Bank
    Bangor ME
    ------------------------------



  • 17.  RE: REST call from IBMi to a MS SQL Server database

    Posted 05/04/26 03:09 AM

    Hi Michael,

    You can use the Qp0lSetAttr API as well to do this instead of using a command to execute.



    ------------------------------
    Paul Nicolay
    Architect
    Cegeka
    ------------------------------



  • 18.  RE: REST call from IBMi to a MS SQL Server database

    Posted 04/10/26 10:19 AM

    and new to Google, too, right? ;-)

    I consider this as a rather good example of directly consuming the CLOB without the need to write it to disk:
    Guru: Consuming A REST Web Service Using SQL And GET - IT Jungle

    If you happen to have a rather unstable webservice on the other side and the folks on the other side keep telling you that everything works fine, using curl or wget in a bash script might be a better solution to retrieve the data and write it to disk, as you can log everything and prove the instability.
    You can read it with SQL afterwards.

    IT Jungle remove preview
    Guru: Consuming A REST Web Service Using SQL And GET - IT Jungle
    The use of web services in the industry continues to grow, and sometimes it can be challenging to find information on how to work with them on IBM i. So, what is a web service? In a nutshell, web services allow systems to communicate via the web using file formats such as JSON and XML.
    View this on IT Jungle >






    ------------------------------
    Anton Gombkötö
    ------------------------------