[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: Help consuming a REST webservice with HTTPAPI.
Eureka!!! It works!!! I am now successfully consuming a REST Web
Service in RPG!
I would like to send a big thank-you to Mike Krebs, Scott Mildenberger,
& Chris Hayden for all of their generous help and, of course, to Scott
Klement for this fantastic tool.
I'd like to share my code with everyone in the hope that it may help
someone else, and/or if Scott felt like using it as an example. Here it
is:
H BNDDIR('HTTPAPI')
*---------------------------------------------------------------------
---*
* DSRTRAFDTA : Fleet Locate consume Traffic Data REST service.
*
* Documentation : [1]https://api.us.spireon.com/doc/
*
*----------------------------------------------------------------------
--*
*
D DSRTRAFDTA PR extpgm('DSRTRAFDTA')
D DSRTRAFDTA PI
*
*---------------------------------------------------------------------
---*
* Prototypes:
*
*---------------------------------------------------------------------
---*
* Scott Klement's HTTPAPI prototype.
/include qrpglesrc,HTTPAPI_H
*---------------------------------------------------------------------
---*
* Definitions:
*
*---------------------------------------------------------------------
---*
*
D userName...
D S 50A
D passWord...
D S 50A
*
D limitEventsReturned...
D S 4P 0
D startAtTimeStamp...
D S 13P 0
*
D rc...
D S 10I 0
D URL...
D S 256A
D debugLog...
D S 1000A varying
*
*---------------------------------------------------------------------
---*
* Mainline:
*
*---------------------------------------------------------------------
---*
*
/free
// Write a Debug log out to the IFS.
debugLog = '/fleetLocate/restTest/debugLog.txt';
http_debug(*on : debugLog);
// Test sandbox credentials.
userName = '********';
password = '********';
// Basic authentication.
http_setAuth( HTTP_AUTH_BASIC
: %trim(userName)
: %trim(passWord));
// Variable fields inserted into the URL.
limitEventsReturned = 50;
startAtTimeStamp = 1403114332938;
// Build the URL. Note I added '.xml. at the end of the word
'traffic' so
// that it would return the data as XML. If you leave off the
'.xml' the
// service will return the data as JSON.
URL = 'https://api.spireon.com/api/asset/traffic.xml'+
'?limit='+
%trim(%char(limitEventsReturned))+
'&startAt='+
%trim(%char(startAtTimestamp));
// GET request.
rc = http_url_get(URL : '/fleetLocate/restTest/trafficData.xml');
// The End.
*inlr = *on;
return;
/end-free
*
You would, of course, have to do the following for this to work on your
machine.
- Replace the '********' entered for Username and Password with actual
credentials.
- Replace '/fleetLocate/restTest/debugLog.txt' with the path where you
want to store the debug log on your machine.
- Replace ' /fleetLocate/restTest/trafficData.xml' with the path where
you want to store the .xml document on your machine.
I realise this is probably pretty simple stuff for most of you, but I
also thought that you can't have too many examples, plus I really
wanted to contribute something back to the group that has helped me so
much.
Have a great day!
___________________________________________________________
Paul Reid
Application Developer III
Erb Group of Companies | 290 Hamilton Road | New Hamburg, Ontario | N3A
1A2
Phone: 519.662.6133 ext. 2363
Web: [2]http://www.erbgroup.com/
From: Mike Krebs <mkrebs@xxxxxxxxxxxxxxxxxx>
To: HTTPAPI and FTPAPI Projects <ftpapi@xxxxxxxxxxxxxxxxxxxxxx>
Date: 24/06/2014 05:29 PM
Subject: RE: Help consuming a REST webservice with HTTPAPI.
Sent by: ftpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx
__________________________________________________________________
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
/free
http_getauthLog = '/tmp/http_getauthLog.txt';
http_debug(*on : http_getauthLog);
http_setAuth( HTTP_AUTH_BASIC
: %trim(Userid)
: %trim(Passwd));
/end-free
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
t","gpsTrackedDistance":1.8484092E7,"heading":221,"id":365871,"idle":fa
lse,"idleStartTime":null,"ignitionOn":false,"ignitionOnStartT
ime":null,"landmarkId":196572,"landmarkName":"newcoke42","lat":34.95623
016357422,"lng":-78.89627838134766,"moving":true,"movingStart
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 [3]http://json.parser.online.fr/
Several JSON parsers available to RPGLE including:
[4]http://www.scottklement.com/yajl/
[5]http://webdefence.global.blackspider.com/urlwrap/?q=AXicY3BnOFnLwPC8
iYGhKKfSwDRDr7ioTC83MTMnOT-vpCg_Ry85P5eh2NTfJyTQLdHAyMLc3IKhoCg1M8UhtSg
pvSi_tACsIqOkpMBKX7-oID0vtaIkPTUPJKqfmZeSWqFXkFFgDzItNa_ENqs4P48BAgAKxC
jO&Z
[6]https://code.google.com/p/powerext/
-----Original Message-----
From: ftpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx
[[7]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')
/free
HeaderData = 'Referer: [8]http://davistransport.com/' + CRLF;
/end-free
p SetReferer e
You would just have two procedures for the two different additional
headers you need.
Scott
-----Original Message-----
From: ftpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx
[[9]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:
----------
Overview
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
[10]https://api.us.spireon.com/api.
Authentication
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]"
[11]https://api.us.spireon.com/api/asset
Your auth string is your username and password combined into a string
and encoded in Base64, like so:
Base64("username:password")
----------
"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
guidelines:
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.
Example
Request:
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:
----------
*----------------------------------------------------------------------
--*
* MYXAMPLE05 - Copied from LIBHTTP/QRPGLESRC/EXAMPLE05 *
*----------------------------------------------------------------------
--*
*
H DFTACTGRP(*NO) ACTGRP(*NEW) BNDDIR('HTTPAPI')
*
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
*
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
*
/free
http_getauthLog = '/fleetLocate/restTest/http_getauthLog.txt';
http_debug(*on : http_getauthLog);
/end-free
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
stuck.
Thanks again in advance, Paul.
___________________________________________________________
Paul Reid
Application Developer III
Erb Group of Companies | 290 Hamilton Road | New Hamburg, Ontario | N3A
1A2
Phone: 519.662.6133 ext. 2363
Web: [12]http://www.erbgroup.com/
-----------------------------------------------------------------------
This is the FTPAPI mailing list. To unsubscribe, please go to:
[13]http://www.scottklement.com/mailman/listinfo/ftpapi
-----------------------------------------------------------------------
-----------------------------------------------------------------------
This is the FTPAPI mailing list. To unsubscribe, please go to:
[14]http://www.scottklement.com/mailman/listinfo/ftpapi
-----------------------------------------------------------------------
References
1. https://api.us.spireon.com/doc/
2. http://www.erbgroup.com/
3. http://json.parser.online.fr/
4. http://www.scottklement.com/yajl/
5. http://webdefence.global.blackspider.com/urlwrap/?q=AXicY3BnOFnLwPC8iYGhKKfSwDRDr7ioTC83MTMnOT-vpCg_Ry85P5eh2NTfJyTQLdHAyMLc3IKhoCg1M8UhtSgpvSi_tACsIqOkpMBKX7-oID0vtaIkPTUPJKqfmZeSWqFXkFFgDzItNa_ENqs4P48BAgAKxCjO&Z
6. https://code.google.com/p/powerext/
7. mailto:ftpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx
8. http://davistransport.com/'
9. mailto:ftpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx
10. https://api.us.spireon.com/api
11. https://api.us.spireon.com/api/asset
12. http://www.erbgroup.com/
13. http://www.scottklement.com/mailman/listinfo/ftpapi
14. http://www.scottklement.com/mailman/listinfo/ftpapi
-----------------------------------------------------------------------
This is the FTPAPI mailing list. To unsubscribe, please go to:
http://www.scottklement.com/mailman/listinfo/ftpapi
-----------------------------------------------------------------------