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

RE: Help consuming a REST webservice with HTTPAPI.

In all the talk about adding the headers...we missed your basic question. Here is how you can do the authentication. You will get JSON back.

Using the program you posted earlier, I made just a couple of changes and got information back. 

I changed pass to passwd just for my own needs. Move the debug to the top of your program (changed the path) and set the authentication before making the http_url_get call.

     D userid          S             50A   INZ('newcoke')
     D passwd          S             50A   INZ('password')
     D URL             s            256A
     D msg             S             50A
     D http_getauthLog...
     D                 S           1000A   varying
     C                   eval      *inlr = *on
          http_getauthLog = '/tmp/http_getauthLog.txt';
          http_debug(*on : http_getauthLog);
          http_setAuth( HTTP_AUTH_BASIC
            : %trim(Userid)
            : %trim(Passwd));

Here is the start of the JSON (found in testAuth.html):

{"success":true,"msg":"Warning: startAt parameter too old. Truncated. Data loaded.","count":49,"data":[{"address":" I- 95","cargoLoa
ded":[{"cargoSensor1":false}],"city":"Fayetteville","deviceId":625350,"eventDateTime":"2014-06-17 21:02:46","eventTypeName":"Movemen
Time":"2014-06-17 20:01:10","name":"23854","serial":"4641005455","speed":104.6071,"state":"NC","stopped":false,"stoppedStartTime":nu
ll,"tractorPower":true,"tractorPowerStartTime":"2014-06-17 19:50:00","zip":"28306"},

If you break that down, you will have your data. Copy and paste to a parser to see what it looks like http://json.parser.online.fr/

Several JSON parsers available to RPGLE including:


-----Original Message-----
From: ftpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx [mailto:ftpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of Scott Mildenberger
Sent: Tuesday, June 24, 2014 10:03 AM
To: HTTPAPI and FTPAPI Projects
Subject: RE: Help consuming a REST webservice with HTTPAPI.

I think you just need to set additional headers, that is what the curl -H is doing.  Here is an example from one of my programs:

       // Set the referer so Mapquest will accept.
       http_xproc( HTTP_POINT_ADDL_HEADER : %paddr(SetReferer));
       rc = http_url_get_raw(Url : 1 : %paddr(CityState) : 60);

And then the procedure:

     p SetReferer      b
     D SetReferer      pi
     D   HeaderData                1024A   varying

     D CRLF            C                   CONST(x'0D25')

       HeaderData = 'Referer: http://davistransport.com/' + CRLF;

     p SetReferer      e            

You would just have two procedures for the two different additional headers you need.


-----Original Message-----
From: ftpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx [mailto:ftpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of PReid@xxxxxxxxxxxx
Sent: Tuesday, June 24, 2014 8:44 AM
To: ftpapi@xxxxxxxxxxxxxxxxxxxxxx
Subject: Help consuming a REST webservice with HTTPAPI.

Hello everyone. 

I need to consume a REST Web service and I'm going to use HTTPAPI to do it. I just need a bit of help getting started. I've consumed SOAP services before with HTTPAPI but this is my first crack at consuming a REST. I've looked at the examples provided with HTTPAPI but I couldn't find one that is doing exactly what this vendor requires. From the vendor's web site here is the pertinent documentation for the service I need to consume:
Spireon API makes it easy to retrieve, create, and update resources on your account through our REST endpoints. The HTTP methods we support are GET, POST, PUT, and DELETE. The base URL endpoint is https://api.us.spireon.com/api.

To make requests to Spireon API, you need to supply the Authorization header for every request you make. We ensure that your credentials are secure because all requests must be made over HTTPS.
Below is a cURL example on how to make a GET request. Note that we require both the Authorization and Account headers to authenticate your request.

% curl  -H "Authorization: Basic [your auth string]"
        -H "Account: [your account ID]"

Your auth string is your username and password combined into a string and encoded in Base64, like so:

"Getting Traffic" is the specific service I want to consume:
Getting Traffic
Fetching account traffic is done through an HTTP GET operation, and 
synchronisation is based on time intervals:
1) The client specifies the startAt timestamp.
2) The server responds with the sequential events from startAt up to a 
limit, and returns the endAt time to the client.
3) The client uses the server endAt marker as its startAt marker for the 
next call.
Performance Considerations
The server limits the response at 2000 events (LIMIT).
The traffic HTTP GET operation can return within a minute with LIMIT 
points in a two hour span. The server prioritizes a fast return over a 
large response, and may set the endAt time before LIMIT.
The system performs best if the client adheres to the following 
1) The client shall not specify start times older than 7 days; the traffic 
server may discard traffic older than 7 days.
2) The client should query traffic in non-overlapping [startAt, endAt[ 
intervals. Server traffic pre-fetching will cause a substantial 
performance hit on out of order or overlapping time intervals. Those shall 
only be used for error recovery.
3) The client should issue back-to-back calls without pause to the server 
until the response is empty.

GET api.spireon.com/api/asset/traffic?limit=50&startAt=1367853959918
I've been given access to a test "sandbox" for this web site with:
Username = newcoke
Password = password
Account ID = 73718
I'm not sure if the "Authentication" part is supposed to be included in 
the GET statement or if the security is done with a separate procedure 
prior to the GET. It's mainly the "Authentication" pert that is holding me 
up.  I've tried playing around with a slightly modified version of 
EXAMPLE05 called MYXAMPLE05 to see if I can figure out what is going on. 
Here is the code I've been working with:
D/copy qrpglesrc,httpapi_h
D cmd             pr                  extpgm('QCMDEXC')
D  command                     200A   const
D  length                       15P 5 const
D rc              S             10I 0
D err             S             10I 0
D basic           S              1N
D digest          S              1N
D realm           S            124A
D userid          S             50A
D pass            S             50A
D URL             s            256A
D msg             S             50A
D http_getauthLog...
D                 S           1000A   varying
C                   eval      *inlr = *on
C                   eval      URL = 'https://api.spireon.com/api' +
C                                   '/asset/traffic?limit=50' +
C                                   '&startAt=1'
C                   dou       rc = 1
C                   eval      rc = http_url_get( URL
C                                : '/fleetLocate/restTest/testAuth.html')
C                   if        rc <> 1
C                   callp     http_error(err)
C                   if        err <> HTTP_NDAUTH
C                   callp     http_crash
C                   return
C                   endif
C                   exsr      getpasswd
C                   endif
C                   enddo
C                   callp     cmd('DSPF '+
C                                '/fleetLocate/restTest/testauth.html'''
C                                 : 200)
 * getpassword - *
Csr   getpasswd     begsr
     http_getauthLog = '/fleetLocate/restTest/http_getauthLog.txt';
     http_debug(*on : http_getauthLog);
C                   eval      rc = http_getauth(basic: digest: realm)
C                   if        rc < 0
C                   eval      msg = HTTP_ERROR
C                   dsply                   msg
C                   return
C                   endif
C                   eval      userid = 'enter userid for ' + realm
C                   dsply                   userid
C                   eval      pass = 'enter passwd for ' + realm
C                   dsply                   pass
C                   if        Digest
C                   callp     http_setauth(HTTP_AUTH_MD5_DIGEST:
C                                          userid: pass)
C                   else
C                   callp     http_setauth(HTTP_AUTH_BASIC:
C                                          userid: pass)
C                   endif
Csr                 endsr
The above code stops on the http_getauth procedure. As you can see I've 
added a debug and here is what it says:
HTTPAPI Ver 1.24 released 2012-01-23
OS/400 Ver V7R1M0

http_getauth(): entered
SetError() #39: Server did not ask for authentication!
The testAuth.html file is blank.

I'm not sure if this is even a good example to use for what I need. I've 
read through the ftpapi@xxxxxxxxxxxxxxxxxxxxxx archives and didn't find 
anything that I thought I could use (maybe I missed something). If anyone 
could give me some help/direction with this is would be greatly 
appreciated as I've been working on this for a few days and I am kind of 

Thanks again in advance, Paul.
Paul Reid
Application Developer III
Erb Group of Companies | 290 Hamilton Road | New Hamburg, Ontario | N3A 
Phone: 519.662.6133 ext. 2363
Web: http://www.erbgroup.com/
This is the FTPAPI mailing list.  To unsubscribe, please go to:
This is the FTPAPI mailing list.  To unsubscribe, please go to: