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

Re: Need simple request help.



Hi John,

> This is a very simple www service.  I don't need to load a file, put
> data into a physical or save information to the IFS.  I just need to
> make the call and get the data back in memory.  I like the currency
> example.  But am at a loss as to how to convert it so that it would work
> for me.

Since your example isn't SOAP (and the currency example is) you'd have 
to do is very differently, but it's not hard.

> This also might not even be the best example to use.  If your address 
 > was lets say the white house I would put in
> http://rpc.geocoder.us/service/csv?address=1600 Pennsylvania Avenue NW,
> Washington,DC

In this example, you're putting all of the data into the URL. The part 
that says "?address=" means that you're passing a "parameter" or 
"variable" named address, and it has a value of "1600 Pennsylvania..."

However, the URL (as you've coded it) is illegal. URLs cannot have 
spaces, and various other characters, in them.  A web browser would 
automatically encode these values for you, but to do the same thing with 
HTTPAPI, you have to call the URL encoder routine.  So you'd have 
something like this:

             address = '1600 W. Pennsylvania Ave NW, '
                     + 'Washington,DC';

             enc = http_url_encoder_new();
             http_url_encoder_addvar( enc
                                    : 'address'
                                    : %addr(address)
                                    : %len(%trimr(address)) );

             url = 'http://rpg.geocoder.us/service/csv?' +
                   http_url_encoder_getstr(enc);

             http_url_encoder_free(enc);

The 'http_url_encoder' stuff basically says "I want to encode a 
parameter named address.  The value of the parameter will be the
contents of the 'address' RPG variable."

Then it adds the 'http://rpc.geocoder' stuff to the front of the
string, and frees the temporary memory used by the encoder.  At this 
point the 'url' variable has the full URL with the address encoded into it.

Now you ask HTTPAPI to retrieve data from that URL:

        rc = http_url_get_raw( url: 0: %paddr(SaveData) );
        if (rc <> 1);
            http_crash();
        endif;

The "http_url_get_raw" API gets (or "retrieves") data from a URL.  The 
standard http_url_get() API will save the results to a file, which isn't 
what you want.  The _raw() variant will, instead, call a subprocedure
that you provide and pass the data (as it's received) as a parameter.

In this example, I've told it to call a procedure named SaveData. 
Because we want to keep the data in memory, SaveData will have to 
translate the data to EBCDIC (since the rpc.geocoder.us site uses ASCII) 
and then save it into an RPG variable.  Here's the code:

      P SaveData        B
      D SaveData        PI            10i 0
      D   fd                          10i 0 value
      D   Data                      8192a   options(*varsize)
      D   len                         10i 0 value
       /free
          http_xlate(len: data: TO_EBCDIC);
          Result = Result + %subst(data:1:Len);
          return len;
       /end-free
      P                 E

Heh... 3 lines of code.  It's pretty easy when you know how :)

Because of this subprocedure, the http_url_get_raw() routine will save 
all of the data it receives into the "Result" variable in my RPG 
program, which is intended to be a VARYING variable.  From there, all 
you have to do is search for the commas to extract the latitude and 
longitude.

Here's the complete sample program  (I may include this with the 
EXAMPLExx members in a future release, if people like the idea)

      H DFTACTGRP(*NO) ACTGRP('GEOTEST') BNDDIR('HTTPAPI')

       /copy HTTPAPI_H

      D SaveData        PR            10i 0
      D   fd                          10i 0 value
      D   Data                      8192a   options(*varsize)
      D   len                         10i 0 value
      D Split           PR           100A   varying
      D    String                   1000A   varying

      D Address         s           1000A
      D Result          s           1000A   Varying
      D url             s          32767A   Varying
      D rc              s             10i 0
      D Enc             s                   like(HTTP_URL_ENCODER)
      D Lat             s             13p 8
      D Lon             s             13p 8

      D COORDSIZE       C                   %len(Lat)
      D COORDDEC        C                   %decpos(Lat)

       /free
             http_debug(*ON);

             address = '1600 W. Pennsylvania Ave NW, '
                     + 'Washington,DC';

             // ---------------------------------------------
             //  Use HTTPAPI's URL encoder to encode the
             //  'address' variable into the URL
             // ---------------------------------------------

             enc = http_url_encoder_new();
             http_url_encoder_addvar( enc
                                    : 'address'
                                    : %addr(address)
                                    : %len(%trimr(address)) );

             url = 'http://rpg.geocoder.us/service/csv?' +
                   http_url_encoder_getstr(enc);

             http_url_encoder_free(enc);


             // ---------------------------------------------
             //  Request the URL from the HTTP server.
             //  We use "get raw" so it calls our "SaveData"
             //  procedure instead of writing the result
             //  to disk.
             // ---------------------------------------------

             Result = '';
             rc = http_url_get_raw( url: 0: %paddr(SaveData) );
             if (rc <> 1);
                http_crash();
             endif;


             Lat = %dec( Split(Result): COORDSIZE: COORDDEC);
             Lon = %dec( Split(Result): COORDSIZE: COORDDEC);

             http_comp('Lat=' + %char(Lat) + ', Lon=' + %char(Lon));
             *inlr = *on;

       /end-free



       *++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       * SaveData():   instead of writing the result to a file,
       *               when it's received from the network,
       *               HTTPAPI will call this routine.
       *
       *               This is called as each network packet
       *               is received from the wire.  Each call
       *               will only contain one small piece of
       *               the data received.  This routine adds
       *               those pieces together.
       *++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      P SaveData        B
      D SaveData        PI            10i 0
      D   fd                          10i 0 value
      D   Data                      8192a   options(*varsize)
      D   len                         10i 0 value
       /free
          http_xlate(len: data: TO_EBCDIC);
          Result = Result + %subst(data:1:Len);
          return len;
       /end-free
      P                 E


       *++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       * Split:  Split a string at the first comma.
       *++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      P Split           B
      D Split           pi           100A   varying
      D    String                   1000A   varying

      D Pos             s             10i 0
      D result          s            100a   varying
       /free

           Pos = %scan(',': String);
           if (Pos = 0);
              Result = String;
              String = '';
              return Result;
           else;
              Result = %subst(String:1:Pos-1);
              String = %subst(String:Pos+1);
           endif;

           return Result;

           begsr *pssr;
              Result = '';
              return Result;
           endsr;
       /end-free
      P                 E
-----------------------------------------------------------------------
This is the FTPAPI mailing list.  To unsubscribe, please go to:
http://www.scottklement.com/mailman/listinfo/ftpapi
-----------------------------------------------------------------------