Hi all
I Can’t make this example work and I don’t know why. need consume one
external Rest webservice from Rpg.
I learn about this technical article and anothers:
https://www.ibm.com/developerworks/ibmi/library/i-send-receive-user-defined-soap-rest-messages-trs/index.html
*First of all I develop one functional prototype. I want consume get
received of this ws (*REST web-service to get a list of all States and
territories of a country):
http://services.groupkt.com/state/get/USA/all
This return a JSON file.
Now I tunning my rpgle based mostly in IBM example:
**free
Ctl-Opt DFTNAME(WSPGM);
/COPY /QIBM/ProdData/OS/WebServices/V1/client/include/Axis.rpgleinc
DCL-S rc INT(10);
DCL-S tHandle POINTER;
DCL-S uri CHAR(200);
DCL-S response CHAR(32768);
DCL-S request CHAR(32768);
DCL-S propBuf CHAR(100);
DCL-S propBuf2 CHAR(100);
// ---------------------------------------------------------------
// Web service logic. The code will attempt to invoke a Web service.
// ---------------------------------------------------------------
// Uncomment to enable trace
// axiscAxisStartTrace('/tmp/axistransport.log': *NULL);
// Set URI
uri = '
http://services.groupkt.com/state/get/USA/all';
// Create HTTP transport handle.
tHandle = axiscTransportCreate(uri:AXISC_PROTOCOL_HTTP11);
if (tHandle = *NULL);
PRINT ('TransportCreate() failed');
return;
endif;
// Now retrieve all student records
PRINT ('==Retrieving list of states');
propBuf = 'GET' + X'00';
axiscTransportSetProperty(tHandle: AXISC_PROPERTY_HTTP_METHOD:
%addr(propBuf));
flushAndReceiveData();
// Cleanup handle.
axiscTransportDestroy(tHandle);
*INLR=*ON;
// =========================================
// Print to standard out
// =========================================
DCL-PROC PRINT ;
dcl-pi *n;
msg varchar(5000) const;
end-pi;
dcl-pr printf extproc(*dclcase);
template pointer value options(*string);
dummy int(10) value options(*nopass);
end-pr;
dcl-c NEWLINE CONST(x'15');
printf(%TRIM(msg) + NEWLINE);
END-PROC PRINT;
// =========================================
// Handle error
// =========================================
DCL-PROC checkError ;
dcl-pi *n;
msg varchar(5000) const;
end-pi;
DCL-S axisCode INT(10);
DCL-S statusCode POINTER;
DCL-S rc INT(10);
axisCode = axiscTransportGetLastErrorCode(tHandle);
PRINT (msg + ' call failed: ' +
%CHAR(axisCode) + ':' +
%STR(axiscTransportGetLastError(tHandle)));
if (axisCode = EXC_TRANSPORT_HTTP_EXCEPTION);
rc = axiscTransportGetProperty(tHandle:
AXISC_PROPERTY_HTTP_STATUS_CODE: %ADDR(statusCode));
PRINT ('HTTP Status code: ' + %STR(statusCode));
endif;
END-PROC checkError;
// =========================================
// Flush and Receive data
// =========================================
DCL-PROC flushAndReceiveData;
dcl-pi *n end-pi;
DCL-S header POINTER;
DCL-S property CHAR(100);
DCL-S bytesRead INT(10) inz(0);
clear response;
clear header;
// Flush data so request is sent
rc = axiscTransportFlush(tHandle);
if (rc = -1);
checkError ('TransportFlush()');
return;
endif;
// Receive data and print out data and response to stdout
rc = axiscTransportReceive(tHandle: %ADDR(response): %SIZE(response):
0);
if (rc = 0);
PRINT ('No data to read');
else;
dow rc > 0 AND bytesRead < %SIZE(response);
bytesRead = bytesRead + rc;
rc = axiscTransportReceive(tHandle:
%ADDR(response)+bytesRead:
%SIZE(response)-bytesRead:
0);
enddo;
endif;
if (rc = -1);
checkError ('TransportReceive()');
elseif (bytesRead > 0);
PRINT ('Bytes read: ' + %CHAR(bytesRead));
PRINT ('Data: ' + response);
endif;
if (rc > -1);
rc = axiscTransportGetProperty(tHandle:
AXISC_PROPERTY_HTTP_STATUS_CODE:
%addr(header));
if (rc = -1);
checkError ('TransportGetProperty()');
else;
PRINT ('HTTP status code: ' + %str(header));
endif;
endif;
END-PROC flushAndReceiveData;
I compile with:
CRTRPGMOD MODULE(<library>/CLIENTR) SRCSTMF('/clientrest.rpgle')
CRTPGM PGM(<library>/CLIENTR) MODULE(<library>/CLIENTR)
BNDSRVPGM((QSYSDIR/QAXIS10CC))
This is my output:
HTTP status code: 400
==Retrieving list of states
Bytes read: 312
Data: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.4.25 (Debian) Server at services.groupkt.com Port
80</address>
</body></html>
HTTP status code: 400
Any help will be appreciated, thanks in advanced.
As an Amazon Associate we earn from qualifying purchases.