IBM Support

ILE RPG CGI programming example

News


Abstract

ILE RPG CGI programming example

Content

You are in: IBM i Technology Updates  > Web Integration on i IBM HTTP Server for i > Introduction > CGI programming example > ILE RPG CGI programming example

This sample code is provided by IBM for illustrative purposes only. It has not been fully tested. It is provided as-is without any warranties of any kind, including but not limited to the implied warranties of merchantability and fitness for a particular purpose.

This program is a simple RPG program that demonstrates the HTTP server APIs for reading standard input, reading an environment variable and writing standard output. This is done using the HTTP Server APIs.

To install the example program, do the following:

  1. Download the following files: 
  2. Use the following CL commands to create a directory structure: 
    1. mkdir dir('/qsys.lib/rpgdemo.lib')
    2. mkdir dir('/qsys.lib/rpgdemo.lib/qcsrc.file')
    3. mkdir dir('/rpgdemo')
    4. mkdir dir('/rpgdemo/html')
      Be aware the mkdir command will use  hierarchical file authorities, unless you specify otherwise, for the *PUBLIC users.  You may want to specifically add read and execute authorities for server user profiles QTMHHTTP and QTMHHTP1 to these new directories.
  3. Use FTP to connect to your server.
  4. Use the following FTP commands to copy the files to your server: 
    1. put samplerpg.html /rpgdemo/html/samplerpg.html
    2. put samplerpg.txt
      /qsys.lib/rpgdemo.lib/qcsrc.file/samplerpg.mbr
  5. Use the following CL commands to create the program (PGM) object called SAMPLERPG: 
    1. CRTRPGMOD MODULE(rpgdemo/samplerpg) SRCFILE(rpgdemo/qcsrc) SRCMBR(samplerpg) OUTPUT(*print)
    2. CRTPGM PGM(rpgdemo/samplerpg) MODULE(rpgdemo/samplerpg) BNDSRVPGM(qhttpsvr/qzhbcgi)
  6. Use your browser to access the HTTP Server Configuration and Administration forms to create an Apache server named, for example, RPGCONFIG.
    • Set the document root to the /rpgdemo/html directory. 
    • Add a directory for the QSYS library allowing access to the directory and allowing CGI script execution, using the Alias and Redirection form. 
    • Add the directory for the QSYS (RPGDEMO.LIB) library, using the Context Managementform.
    • Under Tools, select Display Configuration File to make sure you have the following CGI related directives:

      DocumentRoot /rpgdemo/html/
      Alias /html/ /rpgdemo/html/
      ScriptAlias /cgi-bin/ /QSYS.LIB/RPGDEMO.LIB/
      <Directory /rpgdemo/html/>
          AllowOverride None
          Options None
          order allow,deny
          allow from all
      </Directory>
      <Directory /QSYS.LIB/RPGDEMO.LIB/>
          AllowOverride None
          SetHandler cgi-script
          Options +ExecCGI
          order allow,deny
          allow from all
      </Directory>

  7. Start the RPGCONFIG instance from the GUI.
  8. From your browser type: http://yourservername:port/html/samplerpg.html .

The input for this program comes from CGI standard input or the environment variable QUERY_STRING. For an HTTP request method of POST, the input is read from standard input and for an HTTP request method of GET, the input is read from QUERY_STRING. For method POST, this program will only read 1024 characters. For method GET, the program will not read any input data when it exceeds 1024 characters.

The program reads data from standard input based on the Content_Length environment variable. The QtmhGetEnv System API is used to get the Content_Length and set the InDataLn variable used by the QtmhRdStdIn API. The data to be returned to the client is written to standard output using the QtmhWrStOut API. The data is returned as text/html.

[{"Business Unit":{"code":"BU058","label":"IBM Infrastructure w\/TPS"},"Product":{"code":"SWG60","label":"IBM i"},"Component":"","Platform":[{"code":"PF012","label":"IBM i"}],"Version":"All Versions","Edition":"","Line of Business":{"code":"LOB57","label":"Power"}}]

Document Information

Modified date:
30 January 2020

UID

ibm11171270