ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Accessing REST web services from i-series

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Accessing REST web services from i-series

    Hello

    I need to develop an application in i-series(V7R2) which will need to communicate with REST web services. The communication would be through JSON and the data will be in XML format. It would be GET & PUT both.

    The application needs to constantly poll to get data from the web-service.

    Can anyone pls help me with some good ways(if possible with examples) to achieve this.

    I can provide more information if required.

    Thanks DB

  • #2
    Good morning.
    I use the following tools for this:

    HTTPAPI from Scott Klement and YAJL (ready for i by Scott Klement).

    For the XML-parsing i use the sql-functions (https://www.ibm.com/support/knowledg...df.pdf?view=kc)

    Greets
    Chris

    Comment


    • #3
      If you like to go the SQL way ...
      In the SYSTOOLS library you find all kinds of http functions for accessing web services.
      With a composition of an http functions (httpgetClob) and the XML_TABLE table function you are able to directly read data from an webservice with SQL.

      The following SQL statement will access a web service from the European Central Bank returning exchange rates:


      Code:
      Select  *
       From XMLTable(
              XMLNamespaces(DEFAULT 'http://www.ecb.int/vocabulary/2002-08-01/eurofxref',
                                    'http://www.gesmes.org/xml/2002-08-01' AS "gesmes"),
              'gesmes:Envelope/Cube/Cube/Cube'
              Passing XMLParse(DOCUMENT
                         HTTPGetBLOB('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml',
                                     ''))  
              Columns Subject   VarChar(30) Path '../../../gesmes:subject',
                      Sender    VarChar(30) Path '../../../gesmes:Sender/gesmes:name',
                      ExcDate   Date        Path '../@time',
                      Currency  Char(3)     Path '@currency',
                      ExcRate   Dec(10, 4)  Path '@rate'      ) a;
      If you wrap it in a view, you can easily use this view whereever you can use a table.
      Code:
       Create Or Replace View YOURSCHEMA.YOURVIEWNAME as
       Select  *
       From XMLTable(
              XMLNamespaces(DEFAULT 'http://www.ecb.int/vocabulary/2002-08-01/eurofxref',
                                    'http://www.gesmes.org/xml/2002-08-01' AS "gesmes"),
              'gesmes:Envelope/Cube/Cube/Cube'
              Passing XMLParse(DOCUMENT
                         HTTPGetBLOB('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml',
                                     ''))  
              Columns Subject   VarChar(30) Path '../../../gesmes:subject',
                      Sender    VarChar(30) Path '../../../gesmes:Sender/gesmes:name',
                      ExcDate   Date        Path '../@time',
                      Currency  Char(3)     Path '@currency',
                      ExcRate   Dec(10, 4)  Path '@rate'      ) a;
      No it can be used as follows:
      Code:
      Select * from YOURVIEWNAME;
      Birgitta
      Last edited by B.Hauser; April 27, 2018, 09:01 AM.

      Comment


      • #4
        Hmmm - when I try your SQL Birgitta I get HTTPGETBLOB not found. SQL State is 42704. Have no idea where to look to resolve that.

        To follow up on the earlier post on HTTPAPI and YAJL - Scott has just released an update to YAJL that incorporates a YAJL based parser for DATA-INTO. So XML-INTO and DATA-INTO can also be used to consume the XML and JSON respectively.

        The OP didn't say whether JSON and/or XML needed to be created - YAJL is an excellent way to create the JSON if (like me) you find the SQL needed to create complex documents a little too "read only".

        P.S. Sorry about the red font - can't find how to turn it off in this silly editor.

        Comment


        • wegrace
          wegrace commented
          Editing a comment
          sysTools needs to be in your library list

          or you prefix the HTTPGETBLOB() function with the systools library (eg. systools.HTTPGetBLOB('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml','')

          Walt

      • #5
        Thanks Walt. That works - albeit rather slow but it works. Wonder why on earth that is a requirement - I'd have expected it to be base OS functionality.

        Comment


        • #6
          Only the first call is slow (because the JVM must be started and a FULL Open performed) the subsequent calls are fast.

          Birgitta

          Comment


          • #7
            The other problem with the JVM is the number of resources it uses. I've had people running many jobs with the SQL functions simultaneously, and it basically made the system unusable because the resource usage skyrocketed. They switched to HTTPAPI, and with the same number of jobs, nobody even noticed the resource usage.

            Personally, I also find the SQL syntax to be absolutely horrible and unintuitive... but that's just an opinion.

            HTTPAPI with XML-INTO and YAJL (with DATA-INTO) are a great way to do this stuff.

            One thing in the original post that confuses me is this sentence "The communication would be through JSON and the data will be in XML format." That could use some clarification. I think it has already been established the the communication will be in *HTTP* (JSON is not a communication protocol). But, then how does JSON fit into the picture? Do you mean that JSON will be used to encapsulate XML... if so, why on earth would someone do that? I've seen the opposite (XML encapsulating JSON) but this was for SOAP where XML is required, so if you need a JSON payload that makes some amount of sense. But, the OP specifically metnioned REST... so, I'm baffled... what is really meant by "The communication would be through JSON and the data will be in XML format"?

            Comment

            Working...
            X