IBM Support

UTF-8 CGI programming example



UTF-8 CGI programming example


You are in: IBM i Technology Updates  > Web Integration on i IBM HTTP Server for i > Introduction > CGI programming example > UTF-8 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 and writing standard output. This is done with several containers with different CGIJobCCSIDs using the HTTP Server APIs. The same simple RPG program will be used and HTTP server will change the CCSID for each request.

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/qrpgsrc.file')
    3. mkdir dir('/utf8demo')
    4. mkdir dir('/utf8demo/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 utf8_form.html /utf8demo/html/utf8_form.html
    2. put samplerpg.txt /qsys.lib/rpgdemo.lib/qrpgsrc.file/samplerpg.mbr
  5. Use the following CL commands to create the program (PGM) object called SAMPLERPG: 
    1. CRTRPGMOD MODULE(rpgdemo/samplerpg) SRCFILE(rpgdemo/qrpgsrc) 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, UTF8CONFIG. 
    • Set the document root to the /utf8demo/html directory. 
    • Set the given filename extensions .html and .pgm to the UTF-8 content charset using directive AddCharset. Under Server Properties, select Content Settings and Add file extensions .pgm and .html, select Type Character set and specify value UTF-8.
    • Specify UTF-8 CCSID as the client character set environment that will be used when converting with directive DefaultNetCCSID. Under Server Properties, select General Server Configuration, go to Advanced tab and type 1208 for Client CCSID.
    • Add a directory for the QSYS library allowing access to the directory and allowing CGI script execution and one location for each language with directive CGIJobCCSID in every context with proper EBCDIC CCSID.
    • Add the Script Alias for the QSYS (RPGDEMO.LIB) library for each language, using the URL Mapping form under Server Properties.
    • Under Tools, select Display Configuration File to make sure you have the following CGI related directives:

      DocumentRoot /utf8demo/html/
      AddCharset UTF-8 .html .pgm
      DefaultNetCCSID 1208
      ScriptAlias /cgi-ger /qsys.lib/rpgdemo.lib
      ScriptAlias /cgi-fra /qsys.lib/rpgdemo.lib
      ScriptAlias /cgi-rus /qsys.lib/rpgdemo.lib
      ScriptAlias /cgi-jap /qsys.lib/rpgdemo.lib
      ScriptAlias /cgi-ara /qsys.lib/rpgdemo.lib
      ScriptAlias /cgi-heb /qsys.lib/rpgdemo.lib
      ScriptAlias /cgi-en /qsys.lib/rpgdemo.lib
      <Directory /utf8demo/html/>
          AllowOverride None
          Options None
          order allow,deny
          allow from all
      <Directory /QSYS.LIB/RPGDEMO.LIB/>
          AllowOverride None
          SetHandler cgi-script
          Options +ExecCGI
          order allow,deny
          allow from all
      <Location /cgi-ger/>
          CGIJobCcsid 273
      <Location /cgi-fra/>
          CGIJobCcsid 297
      <Location /cgi-jap/>
          CGIJobCcsid 5035
      <Location /cgi-rus/>
          CGIJobCcsid 1025
      <Location /cgi-en/>
          CGIJobCcsid 37
      <Location /cgi-heb/>
          CGIJobCcsid 424
      <Location /cgi-ara/>
          CGIJobCcsid 420

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

The input for this program comes from CGI standard input. For an HTTP request method of POST, the input is read from the standard input. For method POST, this program will only read 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":"BU054","label":"Systems w\/TPS"},"Product":{"code":"SWG60","label":"IBM i"},"Component":"","Platform":[{"code":"PF012","label":"IBM i"}],"Version":"All Versions","Edition":"","Line of Business":{"code":"LOB08","label":"Cognitive Systems"}}]

Document Information

Modified date:
30 January 2020