Chapter 3 - GETURI ILE Functions (F.GETURI)

Included with v10.10 and higher of GETURI is service program F.GETURI which contains ILE functions that will allow you to build and call GETURI.

Source member QCOPYSRC/P.GETURI contains the RPG prototype definitions for the functions available.

There is also a binding directory named GETURI that includes the F.GETURI service program. You can use this binding directory when using the functions included in the F.GETURI service program or add the F.GETURI service program to your own binding directory.

Available Functions

  1. #geturi_resetValues() - This is the first function to call to make sure all the functions are reset.

  2. #geturi_setValue() - This function is used to set the value of any of the parameters except for User Defined Headers.

  3. #geturi_addHeader() - This function is used to add User Defined Headers to the request.

  4. #geturi_call() - This function will execute the GETURI call.

  5. #geturi_getHeader() - This function will allow you to retrieve a header from the response.

  6. #geturi_getResponseCode() - This function will allow you to retrieve the HTTP response code (ie, 200, 400, etc) and optionally the HTTP response code text.

The P.GETURI /copy Prototype Member

The prototype definitions and descriptions are as follows:

**FREE

////////////////////////////////////////////////////////////////*

// geturi_resetValues - Reset Values *

// *

// This function will reset the values and should be called *

// prior to calling any other functions when making a new call *

// with GETURI. *

// *

// Returns: *

// -1 for error, 0 for no error. *

// *

////////////////////////////////////////////////////////////////*

dcl-pr geturi_resetValues int(10) end-pr;


////////////////////////////////////////////////////////////////*

// geturi_setValue - Set Value of Field *

// *

// This function set the value for a GETURI parameter. The *

// paramter name is case insensitive. *

// *

// Input: *

// in_parameter- The paramter to set (ie. gi_uri) *

// in_value (required) - The value for the parameter. *

// *

// Output: *

// out_errMsg - If an error is encountered this will *

// contain a description of the error. *

// *

// Returns: *

// -1 for error, 0 for no error. *

// *

////////////////////////////////////////////////////////////////*

dcl-pr geturi_setValue int(10);

in_paramter varchar(256) Const;

in_value varchar(65535) Const;

out_errMsg varchar(256) Options(*NOPASS);

end-pr;


////////////////////////////////////////////////////////////////*

// geturi_addHeader - Add Header *

// *

// This function will add a custom heder to the GETURI call. *

// *

// Input: *

// in_header (required) - The header lable *

// in_headerValue (required) - The header value. *

// *

// Output: *

// out_errMsg - If an error is encountered this will *

// contain a description of the error. *

// *

// Returns: *

// -1 for error, n for no error where n is the index of the *

// header just added. *

// *

////////////////////////////////////////////////////////////////*

dcl-pr geturi_addHeader int(10);

in_header varchar(2048) Const;

in_headerValue varchar(65535) Const;

out_errMsg varchar(256) Options(*NOPASS);

end-pr;


////////////////////////////////////////////////////////////////*

// geturi_call - Call GETURI *

// *

// This function is used to call the GETURI function. Before *

// calling this the geturi_resetValues() function should be *

// called along with one or more geturi_setValue() functions *

// to set up any values for the specific call. *

// *

// Output: *

// out_repsonse - The response returned and *RETURN is used *

// for the output type. *

// out_head - The header data from the response. *

// out_msgCd - The message code returned from the call. *

// out_msg - The message returned from the call. *

// out_errMsg - If an error is encountered this will *

// contain a description of the error. *

// *

// Returns: *

// -1 for error, 0 for no error. *

// *

////////////////////////////////////////////////////////////////*

dcl-pr geturi_call int(10);

out_response varchar(1048576);

out_head varchar(1048576);

out_msgCd char(1);

out_msg varchar(2048);

out_errMsg varchar(256) Options(*NOPASS);

end-pr;


////////////////////////////////////////////////////////////////*

// geturi_getHeader - Get header from response. *

// *

// This function is used to retrieve the value of a header *

// after a call has been made. The value will be returned *

// as text. *

// *

// Input: *

// in_header - The name of the header to retrieve. This *

// value is case in-sensitive and should not contain a *

// colon (ie, Content-Type, not Content-Type:) *

// in_instance - The instance of the header to retrieve. *

// This value defaults to 1 if not passed. *

// Output: *

// out_errMsg - If an error is encountered this will *

// contain a description of the error. *

// *

// Returns: *

// The header value, if found. *

// *

////////////////////////////////////////////////////////////////*

dcl-pr geturi_getHeader varchar(65535);

in_header varchar(2048) Const;

in_instance int(10) Options(*NOPASS) Const;

out_errMsg varchar(256) Options(*NOPASS);

end-pr;

////////////////////////////////////////////////////////////////*

// geturi_getResponseCode - Get Response Code from Header *

// *

// This function is used to retrieve the respose code from *

// the HTTP headers. The value will be returned as text. *

// *

// Output: *

// out_responseText - The response text (ie. OK, etc) *

// out_errMsg - If an error is encountered this will *

// contain a description of the error. *

// *

// Returns: *

// The header response code. *

// *

////////////////////////////////////////////////////////////////*

dcl-pr geturi_getResponseCode varchar(32);

out_responseText varchar(256) Options(*NOPASS);

out_errMsg varchar(256) Options(*NOPASS);

end-pr;


Sample Programs

Sample RPGLE and COBOL Programs

There is also a source physical file in library GETURI named QRPGLESRC that contains a sample program.

The following is a sample RPGLE program using the ILE functions:

**FREE

ctl-opt DFTACTGRP(*NO) ACTGRP('GETURI') BNDDIR('GETURI');


// Imports

/COPY QCOPYSRC,GETURICOPY

/COPY QCOPYSRC,P.GETURI


// Work Variables

dcl-s rc int(10);


dcl-s errMsg varchar(256);

dcl-s response varchar(32);

dcl-s contentType varchar(65535);


geturi_resetValues();

geturi_setValue('gi_uri':'www.webservice.com/application/function');

geturi_setValue('gi_port':'443'); //this isn't needed if gi_ssl = *YES

geturi_setValue('gi_ssl':'*YES');

geturi_addHeader('Authorization':'Bearer ' + %trimr(token));

geturi_setValue('gi_outtype':'*STMF');

geturi_setValue('gi_stmf':'/tmp/geturitest.output.txt');

geturi_setValue('gi_debug':'*YES');


rc = geturi_call(GetUri_Out:GetUri_Head:GetUri_MsgCd:GetUri_Msg:errMsg);


response = geturi_getResponseCode();

contentType = geturi_getHeader('content-type');


EXSR $return;


//***************************************************************

//* Return

//***************************************************************

begsr $return;


*INLR = *ON;

return;


endsr;


The following is an example ILE COBOL program (note, I am NOT a COBOL program by any stretch of the imagination :) ) I also would like to thank Jon Paris for working with me on brushing up on my COBOL programming!:

Process Options NOMONOPRC NOSTDTRUNC

Identification Division.

Program-Id. GFTEST1.

Data Division.

Working-Storage Section.

01 rc pic 9(9) Binary.

01 errMsg pic x(256).


01 GetURISetValueData.

05 in_parm pic x(256).

05 in_value pic x(65535).


01 GetURIAddHeader.

05 in_header pic x(128).

05 in_headerValue pic x(2048).


01 GetURiCallData.

05 out_response pic x(65535).

05 out_head pic x(65535).

05 out_msgCd pic x.

05 out_msg pic x(65535).


Procedure Division.

Main.


Call linkage procedure "geturi_RESETVALUES"

Returning rc.


move "gi_uri" to in_parm.

move "www.google.com" to in_value.


Call linkage procedure "geturi_SETVALUE"

Using by value in_parm, by value in_value, errMsg

Returning rc.


move "gi_port" to in_parm.

move "443" to in_value.


Call linkage procedure "geturi_SETVALUE"

Using by value in_parm, by value in_value, errMsg

Returning rc.


move "gi_ssl" to in_parm.

move "*YES" to in_value.


Call linkage procedure "geturi_SETVALUE"

Using by value in_parm, by value in_value, errMsg

Returning rc.


move "gi_outtype" to in_parm.

move "*STMF" to in_value.


Call linkage procedure "geturi_SETVALUE"

Using by value in_parm, by value in_value, errMsg

Returning rc.


move "gi_stmf" to in_parm.

move "/tmp/COBOL_geturiout.txt" to in_value.


Call linkage procedure "geturi_SETVALUE"

Using by value in_parm, by value in_value, errMsg

Returning rc.


move "gi_debug" to in_parm.

move "*YES" to in_value.


Call linkage procedure "geturi_SETVALUE"

Using by value in_parm, by value in_value, errMsg

Returning rc.


Call linkage procedure "geturi_CALL"

Using out_response, out_head,

out_msgCd, out_Msg, errMsg

Returning rc.


Stop Run.