[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: REST with JSON returned



The example attached is not Json specific, but it demonstrates how to use http_url_post_raw() to receive the response into a buffer. The data is received as utf-8 or whatever encoding the server used to send the data. Therefore you may need to translate it to EBCDIC (depending on your needs).

Thomas.

-----Ursprüngliche Nachricht-----
Von: ftpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx [mailto:ftpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx] Im Auftrag von Hayes, Daniel
Gesendet: Donnerstag, 18. Februar 2016 15:11
An: ftpapi@xxxxxxxxxxxxxxxxxxxxxx
Betreff: REST with JSON returned

I am trying to consume a RESTful web service where the response will be in this format:

{"ApNumber":32866062,"ApAmount":115.291}

This is all I will get back.  I have gone through all of Scott's example programs and they always load the return into a file on the IFS, I just want it returned in a variable so I can parse this out.  I started out using Cozzi's iSockets, but he does not handle SSL.  Using that I just used 'GetURLData'.  Is there an equivalent in HTTPAPI?  Seems a little awkward to have my response go to the IFS and then have to pull it back in.

Thanks,
Danny Hayes


________________________________
This communication is intended only for the use of the individual or entity named as the addressee. It may contain information which is privileged and/or confidential under applicable law. If you are not the intended recipient or such recipient's employee or agent, you are hereby notified that any dissemination, copy or disclosure of this communication is strictly prohibited. If you have received this communication in error, please immediately notify CareCentrix Compliance Hot Line at (877) 848-8229 and notify the sender by electronic mail. Please expunge this communication without making any copies. Thank you for your cooperation.
--
IMPORTANT NOTICE:
This email is confidential, may be legally privileged, and is for the intended recipient only. Access, disclosure, copying, distribution, or reliance on any of it by anyone else is prohibited and may be a criminal offence. Please delete if obtained in error and email confirmation to the sender.
      *  This is an example of using http_url_post_raw() for
      *  calling a SOAP Web service w/HTTPAPI.
      *
      *  This sample calls the Currency Exchange Rate Web service
      *  provided by WebserviceX.net. For more info, search for it
      *  at   http://www.WebserviceX.net
      *
      *  To Compile (requires V5R1):
      *     CRTBNDRPG PGM(EXAMPLE28) SRCFILE(libhttp/QRPGLESRC)
      *
      *  To Run:
      *  (This shows the value of USD 12.00 in Japanese currency.)
      *
      *  (This shows the conversion rate from USD to Japanese currency.)
      *
     H DFTACTGRP(*NO) BNDDIR('HTTPAPI':'QC2LE')

     D EXAMPLE28       PR                  ExtPgm('EXAMPLE28')
     D   Country1                     3A   const
     D   Country2                     3A   const
     D   Amount                      15P 5 const
     D EXAMPLE28       PI
     D   Country1                     3A   const
     D   Country2                     3A   const
     D   Amount                      15P 5 const

      /copy httpapi_h

     D receiveProc     PR            10I 0
     D  fd                           10I 0 value
     D  data                           *   value
     D  length                       10I 0 value

     D receiveBuffer   ds                  qualified
     D  size                         10I 0 inz
     D  data                      32767A   inz

     D sizeEbcdic      s             10I 0
     D ebcdic          s                   like(receiveBuffer.data)
     D                                     based(pEbcdic)

     D SOAP            s          32767A   varying
     D rc              s             10I 0
     D s               s             10I 0
     D l               s             10I 0

     D rate            s              8F
     D Result          s             12P 2

     D atof            PR             8F   extproc('atof')
     D   string                        *   value options(*string)

      /free

       if ( %parms < 3 );
          http_comp( 'Please pass parms. e.g. CALL EXAMPLE28 '
                   + 'PARM(USD JPY 12.00)');
          return;
       endif;

       // Note:  http_debug(*ON/*OFF) can be used to turn debugging
       //        on and off.  When debugging is turned on, diagnostic
       //        info is written to an IFS file named
       //        /tmp/httpapi_debug.txt

       http_debug(*ON);

       // Note:  http_XmlStripCRLF(*ON/*OFF) controls whether or not
       //        the XML parser removes CR and LF characters from the
       //        Xml data that's passed to your 'Incoming' procedure.

       http_XmlStripCRLF(*ON);

       SOAP =
        '<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>'
       +'<SOAP:Envelope'
       +'    xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/";'
       +'    xmlns:tns="http://www.webserviceX.NET/";>'
       +'<SOAP:Body>'
       +'  <tns:ConversionRate>'
       +'      <tns:FromCurrency>'+ %trim(Country1) +'</tns:FromCurrency>'
       +'      <tns:ToCurrency>'+ %trim(Country2) + '</tns:ToCurrency>'
       +'  </tns:ConversionRate>'
       +'</SOAP:Body>'
       +'</SOAP:Envelope>';

       rc = http_url_post_raw(
                  'http://www.webservicex.net/CurrencyConvertor.asmx'
                         : %addr(SOAP) + 2
                         : %len(SOAP)
                         : -1
                         : %paddr(receiveProc)
                         : HTTP_TIMEOUT
                         : HTTP_USERAGENT
                         : 'text/xml'
                         : 'http://www.webserviceX.NET/ConversionRate');

       if (rc <> 1);
          http_crash();
       else;
          // Translate the received data to EBCDIC and
          // free the memory allocated by HTTP_xlatedyn().
          sizeEbcdic =
             HTTP_xlatedyn(receiveBuffer.size: %addr(receiveBuffer.data)
                           : TO_EBCDIC: pEbcdic);
          receiveBuffer.data = %subst(ebcdic: 1: sizeEbcdic);
          receiveBuffer.size = sizeEbcdic;
          dealloc(n) pEbcdic; // Do not forget that!

          // Retrieve the conversion rate from the response message.
          s = %scan('<ConversionRateResult>': receiveBuffer.data) +
              %len('<ConversionRateResult>');
          l = %scan('</ConversionRateResult>': receiveBuffer.data) - s;
          rate = atof(%subst(receiveBuffer.data: s: l));

          // Be carefully, when using atof(), because atof() expects the
          // decimal point in the locale of the job!
          // Today it is better to use %float(), because it works with
          // a period (.) and a comma (,).

          // Compute the result.
          Result = %dech(Amount * rate: 12: 2);
          http_comp(%trim(Country1) + ' ' + %char(%dec(Amount:12:2))
                    + ' = ' + %trim(Country2) + ' '+ %char(Result));
       endif;

       *inlr = *on;

      /end-free

      *
      * Receives the data from the web service and stores it
      * into 'receiveBuffer'. The data is received in the
      * encoding of the web service and must be translated to
      * EBCDIC according to your needs.
      * See HTTP_xlatedyn() after http_url_post_raw().
      *
     P receiveProc     B
     D                 PI            10I 0
     D  fd                           10I 0 value
     D  data                           *   value
     D  length                       10I 0 value

     D inBuffer        S                   like(receiveBuffer.data)
     D                                     based(data)
     D size            S             10I 0

      /free

       if (receiveBuffer.size + length > %len(receiveBuffer.data));
          size = %len(receiveBuffer.data) - receiveBuffer.size;
       else;
          size = length;
       endif;

       %subst(receiveBuffer.data: receiveBuffer.size + 1: size) =
          %subst(inBuffer: 1: size);

       receiveBuffer.size = receiveBuffer.size + size;

       return size;

      /end-free
     P                 E
-----------------------------------------------------------------------
This is the FTPAPI mailing list.  To unsubscribe, please go to:
http://www.scottklement.com/mailman/listinfo/ftpapi
-----------------------------------------------------------------------